Skip to content

Commit 0023bb0

Browse files
committed
Check if multierror is nil in WrappedErrors
This adds a check similar to `ErrorsOrNil` that ensures the multierror pointer is not nil before returning the Errors field. I believe this is fully backwards-compatible, because the former behavior is a runtime panic.
1 parent ab6846a commit 0023bb0

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)