Skip to content

Commit

Permalink
fix: consistent handling of nil passed to any wrapping function
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sumin committed Mar 19, 2024
1 parent 541a584 commit 5ea0bbc
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ func New(message string, details ...any) error {

// Wrap returns a new errkitError that has the given message and err as the cause.
func Wrap(err error, message string, details ...any) error {
if err == nil {
return nil
}

e := newError(errors.New(message), 2, details...)
e.cause = err
return e
Expand Down
10 changes: 10 additions & 0 deletions errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,16 @@ func TestErrorsWrapping(t *testing.T) {
if wrappedErr != nil {
t.Errorf("nil expected to be returned")
}

wrappedErr = errkit.Wrap(nil, "Some message")
if wrappedErr != nil {
t.Errorf("nil expected to be returned")
}

wrappedErr = errkit.WithStack(nil)
if wrappedErr != nil {
t.Errorf("nil expected to be returned")
}
})
}

Expand Down

0 comments on commit 5ea0bbc

Please sign in to comment.