Package errors:

- implement GOROOT/src/errors Is interface
- fix bug with GOROOT/src/errors UnWrap / Is
This commit is contained in:
Nicolas JUHEL
2025-02-28 17:37:31 +01:00
parent 0c1f4675fb
commit 1528588499
2 changed files with 16 additions and 12 deletions

View File

@@ -86,6 +86,14 @@ func (e *ers) is(err *ers) bool {
return false return false
} }
func (e *ers) Is(err error) bool {
if er, ok := err.(*ers); ok {
return e.is(er)
} else {
return e.IsError(err)
}
}
func (e *ers) Add(parent ...error) { func (e *ers) Add(parent ...error) {
for _, v := range parent { for _, v := range parent {
if v == nil { if v == nil {
@@ -124,7 +132,7 @@ func (e *ers) IsCode(code CodeError) bool {
} }
func (e *ers) IsError(err error) bool { func (e *ers) IsError(err error) bool {
return e.e == err.Error() return strings.EqualFold(e.e, err.Error())
} }
func (e *ers) HasCode(code CodeError) bool { func (e *ers) HasCode(code CodeError) bool {
@@ -286,25 +294,18 @@ func (e *ers) GetErrorSlice() []error {
} }
func (e *ers) Unwrap() []error { func (e *ers) Unwrap() []error {
var r = []error{ if len(e.p) < 1 {
&ers{ return nil
c: e.c,
e: e.e,
p: nil,
t: e.t,
},
} }
if len(e.p) < 1 { var r = make([]error, 0)
return r
}
for _, v := range e.p { for _, v := range e.p {
if v == nil { if v == nil {
continue continue
} }
r = append(r, v.Unwrap()...) r = append(r, v)
} }
return r return r

View File

@@ -49,6 +49,9 @@ type Error interface {
//GetParentCode return a slice of CodeError value of all parent Error and the code of the current Error //GetParentCode return a slice of CodeError value of all parent Error and the code of the current Error
GetParentCode() []CodeError GetParentCode() []CodeError
//Is implement compatiblity with GOROOT/src/errors/wrap Is fucntion
Is(e error) bool
//IsError check if the given error params is a valid error and not a nil pointer //IsError check if the given error params is a valid error and not a nil pointer
IsError(e error) bool IsError(e error) bool
//HasError check if the given error in params is still in parent error //HasError check if the given error in params is still in parent error