Skip to content

Commit

Permalink
Make fault.Wrap nil tolerant #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Southclaws committed Nov 30, 2022
1 parent a79d295 commit 9a66f84
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 18 deletions.
2 changes: 1 addition & 1 deletion fault.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Wrapper func(err error) error
// Wrap wraps an error with all of the wrappers provided.
func Wrap(err error, w ...Wrapper) error {
if err == nil {
panic("nil error passed to Wrap")
return nil
}

for _, fn := range w {
Expand Down
2 changes: 1 addition & 1 deletion fctx/fctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func WithMeta(ctx context.Context, kv ...string) context.Context {
// }
func Wrap(err error, ctx context.Context, kv ...string) error {
if err == nil {
panic("nil error passed to Wrap")
return nil
}

meta, ok := ctx.Value(contextKey{}).(map[string]string)
Expand Down
2 changes: 1 addition & 1 deletion fmsg/fmsg.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type withMessage struct {
// punctuation and grammar and end the message with a period.
func Wrap(err error, internal, external string) error {
if err == nil {
panic("nil error passed to Wrap")
return nil
}

return &withMessage{
Expand Down
2 changes: 1 addition & 1 deletion ftag/ftag.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (e *withKind) String() string { return e.Error() }
// Wrap wraps an error and gives it a distinct tag.
func Wrap(parent error, k Kind) error {
if parent == nil {
panic("nil error passed to Wrap")
return nil
}

if k == "" {
Expand Down
8 changes: 0 additions & 8 deletions tests/fmsg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package tests

import (
"errors"
"fmt"
"testing"

"github.com/Southclaws/fault/fmsg"
Expand Down Expand Up @@ -45,10 +44,3 @@ func TestWithManySlice(t *testing.T) {
assert.Len(t, out, 3)
assert.Equal(t, []string{"Your reply draft has been saved however we could not publish it.", "Unable to reply to post.", "The post was not found."}, out)
}

func TestNil(t *testing.T) {
assert.Panics(t, func() {
err := fmsg.Wrap(nil, "oh no", ":(")
fmt.Println(err)
})
}
6 changes: 0 additions & 6 deletions tests/ftag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,3 @@ func TestWrapWithKindChanging(t *testing.T) {

assert.Equal(t, ftag.NotFound, out, "Should always pick the most recent kind from an error chain.")
}

func TestWrapNil(t *testing.T) {
assert.Panics(t, func() {
ftag.Wrap(nil, ftag.NotFound)
})
}

0 comments on commit 9a66f84

Please sign in to comment.