Skip to content

Commit

Permalink
Bugfix error stringify
Browse files Browse the repository at this point in the history
  • Loading branch information
iychoi committed Mar 8, 2023
1 parent f2851b9 commit d5dbebc
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
14 changes: 9 additions & 5 deletions irods/common/error_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -1377,23 +1377,27 @@ func init() {

func SplitIRODSErrorCode(code ErrorCode) (ErrorCode, LinuxErrorCode) {
mainErrCode := (code / 1000) * 1000
subErrCode := (code / 1000)
subErrCode := (code % 1000)

return mainErrCode, LinuxErrorCode(subErrCode)
}

// GetIRODSErrorString returns string representation of error code
func GetIRODSErrorString(code ErrorCode) string {
if code < 0 {
code = -1 * code
if code == 0 {
return ""
}

mainErrCode, subErrCode := SplitIRODSErrorCode(code)

if mainErrCode > 0 {
if mainErrCode != 0 {
if mainErrCode > 0 {
mainErrCode *= -1
}

mainErrString, ok := errorCodeDescriptionTable[mainErrCode]
if ok {
if subErrCode > 0 {
if subErrCode != 0 {
subErrString := GetLinuxErrorString(subErrCode)
return fmt.Sprintf("%s (sub %s)", mainErrString, subErrString)
}
Expand Down
4 changes: 4 additions & 0 deletions irods/common/linux_error_code.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ func init() {

// GetLinuxErrorString returns string representation of error code
func GetLinuxErrorString(code LinuxErrorCode) string {
if code == 0 {
return ""
}

if code < 0 {
code = -1 * code
}
Expand Down
37 changes: 37 additions & 0 deletions test/testcases/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package testcases

import (
"testing"

"github.com/cyverse/go-irodsclient/irods/common"
"github.com/stretchr/testify/assert"
)

func TestError(t *testing.T) {
t.Run("test ErrorString", testErrorString)
}

func testErrorString(t *testing.T) {
errcode := common.REMOTE_SERVER_AUTHENTICATION_FAILURE

// test - value
errstr := common.GetIRODSErrorString(errcode)
assert.Contains(t, errstr, "REMOTE_SERVER_AUTHENTICATION_FAILURE")

// test + value
errstr = common.GetIRODSErrorString(common.ErrorCode(-1 * int(errcode)))
assert.Contains(t, errstr, "REMOTE_SERVER_AUTHENTICATION_FAILURE")

// test sub value
errcode = common.ErrorCode(int(common.REMOTE_SERVER_AUTHENTICATION_FAILURE) - int(common.EIO))
assert.Equal(t, int(errcode), -910005)

mainErrcode, subErrcode := common.SplitIRODSErrorCode(errcode)
assert.Equal(t, common.REMOTE_SERVER_AUTHENTICATION_FAILURE, mainErrcode)
assert.Equal(t, -1*common.EIO, subErrcode)

errstr = common.GetIRODSErrorString(errcode)
assert.Contains(t, errstr, "REMOTE_SERVER_AUTHENTICATION_FAILURE")
assert.Contains(t, errstr, "I/O error")

}

0 comments on commit d5dbebc

Please sign in to comment.