mirror of
https://gitee.com/johng/gf.git
synced 2024-11-29 18:57:44 +08:00
fix error in gerror.HasCode
(#3277)
This commit is contained in:
parent
4fb739615b
commit
e6d61e6e14
@ -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)
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user