Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: consistent handling of nil passed to any wrapping function #4

Merged
merged 2 commits into from
Mar 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func Foo() error {

### Adding stack trace
If you are interested in adding information about the line and filename where the sentinel error happened, you can do the following:

**NOTE**: While the `WithStack` will return `nil` if passed `err` equals `nil`, we do not consider this good practice and recommend checking the `err` value before invocation.
```go
func Foo() error {
...
Expand All @@ -47,6 +49,8 @@ func Bar() error {

### Adding error cause information
Sometimes you might be interested in returning a sentinel error, but also add some cause error to it, in such cases you can do the following:

**NOTE**: While the `WithCause` will return `nil` if passed `err` equals `nil`, we do not consider this good practice and recommend checking the `err` value before invocation.
```go
func FetchSomething(ID string) error {
err := doSomething() // Here we have an error
Expand Down Expand Up @@ -74,6 +78,8 @@ func FooBar() error {

### Wrapping an error with a high-level message
Sometimes you might want to add some high-level information to an error before passing it up to the invoker.

**NOTE**: While the `Wrap` will return `nil` if passed `err` equals `nil`, we do not consider this good practice and recommend checking the `err` value before invocation.
```go
func LoadProfile() error {
err := makeAnApiCall()
Expand Down
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
Loading