diff --git a/errors/gerror/gerror_api_code.go b/errors/gerror/gerror_api_code.go index e4e4a2b63..98ac32821 100644 --- a/errors/gerror/gerror_api_code.go +++ b/errors/gerror/gerror_api_code.go @@ -109,7 +109,7 @@ func WrapCodeSkipf(code gcode.Code, skip int, err error, format string, args ... } } -// Code returns the error code of current error. +// Code returns the error code of `current error`. // It returns `CodeNil` if it has no error code neither it does not implement interface Code. func Code(err error) gcode.Code { if err == nil { @@ -129,8 +129,8 @@ func HasCode(err error, code gcode.Code) bool { if err == nil { return false } - if e, ok := err.(ICode); ok { - return code == e.Code() + if e, ok := err.(ICode); ok && code == e.Code() { + return true } if e, ok := err.(IUnwrap); ok { return HasCode(e.Unwrap(), code) diff --git a/errors/gerror/gerror_z_unit_test.go b/errors/gerror/gerror_z_unit_test.go index 71a1b4fc7..cb892c401 100644 --- a/errors/gerror/gerror_z_unit_test.go +++ b/errors/gerror/gerror_z_unit_test.go @@ -433,10 +433,14 @@ func Test_HashCode(t *testing.T) { err2 := gerror.WrapCode(gcode.CodeNotAuthorized, err1, "2") err3 := gerror.Wrap(err2, "3") err4 := gerror.Wrap(err3, "4") + err5 := gerror.WrapCode(gcode.CodeInvalidParameter, err4, "5") t.Assert(gerror.HasCode(err1, gcode.CodeNotAuthorized), false) t.Assert(gerror.HasCode(err2, gcode.CodeNotAuthorized), true) t.Assert(gerror.HasCode(err3, gcode.CodeNotAuthorized), true) t.Assert(gerror.HasCode(err4, gcode.CodeNotAuthorized), true) + t.Assert(gerror.HasCode(err5, gcode.CodeNotAuthorized), true) + t.Assert(gerror.HasCode(err5, gcode.CodeInvalidParameter), true) + t.Assert(gerror.HasCode(err5, gcode.CodeInternalError), false) }) }