Skip to content

Commit 2752720

Browse files
authored
Fix uninitialized error detection
Incorporate fix for go-errors#36
1 parent 33d496f commit 2752720

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

error.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func New(e interface{}) *Error {
9191
// fmt.Errorf("%v"). The skip parameter indicates how far up the stack
9292
// to start the stacktrace. 0 is from the current call, 1 from its caller, etc.
9393
func Wrap(e interface{}, skip int) *Error {
94-
if e == nil {
94+
if IsUninitialized(e) {
9595
return nil
9696
}
9797

@@ -114,6 +114,20 @@ func Wrap(e interface{}, skip int) *Error {
114114
}
115115
}
116116

117+
// IsUninitialized returns true if the error is nil or is zero value
118+
func IsUninitialized(i interface{}) bool {
119+
if i == nil {
120+
return true
121+
}
122+
switch reflect.TypeOf(i).Kind() {
123+
case reflect.Struct:
124+
return reflect.ValueOf(i).IsZero()
125+
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
126+
return reflect.ValueOf(i).IsNil()
127+
}
128+
return false
129+
}
130+
117131
// WrapPrefix makes an Error from the given value. If that value is already an
118132
// error then it will be used directly, if not, it will be passed to
119133
// fmt.Errorf("%v"). The prefix parameter is used to add a prefix to the

0 commit comments

Comments
 (0)