Skip to content

Commit 9a66f84

Browse files
committed
Make fault.Wrap nil tolerant #20
1 parent a79d295 commit 9a66f84

File tree

6 files changed

+4
-18
lines changed

6 files changed

+4
-18
lines changed

fault.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ type Wrapper func(err error) error
1818
// Wrap wraps an error with all of the wrappers provided.
1919
func Wrap(err error, w ...Wrapper) error {
2020
if err == nil {
21-
panic("nil error passed to Wrap")
21+
return nil
2222
}
2323

2424
for _, fn := range w {

fctx/fctx.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func WithMeta(ctx context.Context, kv ...string) context.Context {
9090
// }
9191
func Wrap(err error, ctx context.Context, kv ...string) error {
9292
if err == nil {
93-
panic("nil error passed to Wrap")
93+
return nil
9494
}
9595

9696
meta, ok := ctx.Value(contextKey{}).(map[string]string)

fmsg/fmsg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type withMessage struct {
2323
// punctuation and grammar and end the message with a period.
2424
func Wrap(err error, internal, external string) error {
2525
if err == nil {
26-
panic("nil error passed to Wrap")
26+
return nil
2727
}
2828

2929
return &withMessage{

ftag/ftag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func (e *withKind) String() string { return e.Error() }
2020
// Wrap wraps an error and gives it a distinct tag.
2121
func Wrap(parent error, k Kind) error {
2222
if parent == nil {
23-
panic("nil error passed to Wrap")
23+
return nil
2424
}
2525

2626
if k == "" {

tests/fmsg_test.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package tests
22

33
import (
44
"errors"
5-
"fmt"
65
"testing"
76

87
"github.com/Southclaws/fault/fmsg"
@@ -45,10 +44,3 @@ func TestWithManySlice(t *testing.T) {
4544
assert.Len(t, out, 3)
4645
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)
4746
}
48-
49-
func TestNil(t *testing.T) {
50-
assert.Panics(t, func() {
51-
err := fmsg.Wrap(nil, "oh no", ":(")
52-
fmt.Println(err)
53-
})
54-
}

tests/ftag_test.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,3 @@ func TestWrapWithKindChanging(t *testing.T) {
2626

2727
assert.Equal(t, ftag.NotFound, out, "Should always pick the most recent kind from an error chain.")
2828
}
29-
30-
func TestWrapNil(t *testing.T) {
31-
assert.Panics(t, func() {
32-
ftag.Wrap(nil, ftag.NotFound)
33-
})
34-
}

0 commit comments

Comments
 (0)