From d5dbebcd5930722574a085b1d70d93473e9912a0 Mon Sep 17 00:00:00 2001 From: Illyoung Choi Date: Wed, 8 Mar 2023 16:28:30 -0700 Subject: [PATCH] Bugfix error stringify --- irods/common/error_code.go | 14 +++++++----- irods/common/linux_error_code.go | 4 ++++ test/testcases/error_test.go | 37 ++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 test/testcases/error_test.go diff --git a/irods/common/error_code.go b/irods/common/error_code.go index c63ed1b..9b96d33 100644 --- a/irods/common/error_code.go +++ b/irods/common/error_code.go @@ -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) } diff --git a/irods/common/linux_error_code.go b/irods/common/linux_error_code.go index cea8905..6257015 100644 --- a/irods/common/linux_error_code.go +++ b/irods/common/linux_error_code.go @@ -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 } diff --git a/test/testcases/error_test.go b/test/testcases/error_test.go new file mode 100644 index 0000000..7e1ac33 --- /dev/null +++ b/test/testcases/error_test.go @@ -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") + +}