Skip to content

Commit 9974e9e

Browse files
authored
Merge pull request #50 from sethvargo/sethvargo/npe
Check if multierror is nil in WrappedErrors
2 parents ab6846a + 0023bb0 commit 9974e9e

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

multierror.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,17 @@ func (e *Error) GoString() string {
4040
return fmt.Sprintf("*%#v", *e)
4141
}
4242

43-
// WrappedErrors returns the list of errors that this Error is wrapping.
44-
// It is an implementation of the errwrap.Wrapper interface so that
45-
// multierror.Error can be used with that library.
43+
// WrappedErrors returns the list of errors that this Error is wrapping. It is
44+
// an implementation of the errwrap.Wrapper interface so that multierror.Error
45+
// can be used with that library.
4646
//
47-
// This method is not safe to be called concurrently and is no different
48-
// than accessing the Errors field directly. It is implemented only to
49-
// satisfy the errwrap.Wrapper interface.
47+
// This method is not safe to be called concurrently. Unlike accessing the
48+
// Errors field directly, this function also checks if the multierror is nil to
49+
// prevent a null-pointer panic. It satisfies the errwrap.Wrapper interface.
5050
func (e *Error) WrappedErrors() []error {
51+
if e == nil {
52+
return nil
53+
}
5154
return e.Errors
5255
}
5356

multierror_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ func TestErrorWrappedErrors(t *testing.T) {
6969
if !reflect.DeepEqual(multi.Errors, multi.WrappedErrors()) {
7070
t.Fatalf("bad: %s", multi.WrappedErrors())
7171
}
72+
73+
multi = nil
74+
if err := multi.WrappedErrors(); err != nil {
75+
t.Fatalf("bad: %#v", multi)
76+
}
7277
}
7378

7479
func TestErrorUnwrap(t *testing.T) {

0 commit comments

Comments
 (0)