Skip to content

Commit

Permalink
cm fix erro wraping of stderrors
Browse files Browse the repository at this point in the history
  • Loading branch information
deankarn committed Aug 16, 2023
1 parent 2ff27f9 commit 56976f0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
17 changes: 14 additions & 3 deletions chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ func newLink(err error, prefix string, skipFrames int) *Link {
Prefix: prefix,
Source: runtimeext.StackLevel(skipFrames),
}

}

// Chain contains the chained errors, the links, of the chains if you will
Expand Down Expand Up @@ -214,7 +213,16 @@ func (c Chain) Unwrap() error {

// Is reports whether any error in error chain matches target.
func (c Chain) Is(target error) bool {
return stderrors.Is(c[len(c)-1].Err, target)
if len(c) == 0 {
return false
}
if innerErr, ok := target.(Chain); ok {
if len(innerErr) == 0 {
return false
}
target = innerErr[0].Err
}
return stderrors.Is(c[0].Err, target)
}

// As finds the first error in the error chain that matches target, and if so, sets
Expand All @@ -234,7 +242,10 @@ func (c Chain) Is(target error) bool {
// As panics if target is not a non-nil pointer to either a type that implements
// error, or to any interface type.
func (c Chain) As(target any) bool {
return stderrors.As(c[len(c)-1].Err, target)
if len(c) == 0 {
return false
}
return stderrors.As(c[0].Err, target)
}

func defaultFormatFn(c Chain) string {
Expand Down
5 changes: 4 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ func wrap(err error, prefix string, skipFrames int) (c Chain) {
if c, ok = err.(Chain); ok {
c = append(c, newLink(nil, prefix, skipFrames))
} else {
c = Chain{newLink(err, prefix, skipFrames)}
c = Chain{newLink(err, "", skipFrames)}
if prefix != "" {
c = append(c, &Link{Prefix: prefix, Source: c[0].Source})
}
for _, h := range helpers {
if !h(c, err) {
break
Expand Down

0 comments on commit 56976f0

Please sign in to comment.