Skip to content

fix: treat typed nils as nil in Wrap/WrapPrefix#51

Open
Solaris-star wants to merge 1 commit into
go-errors:masterfrom
Solaris-star:fix/35-typed-nil-wrap
Open

fix: treat typed nils as nil in Wrap/WrapPrefix#51
Solaris-star wants to merge 1 commit into
go-errors:masterfrom
Solaris-star:fix/35-typed-nil-wrap

Conversation

@Solaris-star

Copy link
Copy Markdown

Summary

Wrap / WrapPrefix only checked e == nil. A typed nil (var err *MyError) is a non-nil interface value in Go, so it was still wrapped. Calling Error() on the nil pointer then panics.

var err *MyError
errors.WrapPrefix(err, "prefix", 0) // panics before; returns nil after

Fix

Add isNil via reflect for nilable kinds (Ptr, Slice, Map, Func, Chan, Interface) and use it in Wrap / WrapPrefix.

Zero-value structs that implement error are still wrapped (they are not nil).

Test plan

  • TestWrapTypedNil (with examples temporarily ignored locally due to pre-existing Example* vet issues on newer Go)
  • manual repro: typed nil pointer no longer panics

Fixes #35

Wrap and WrapPrefix only checked e == nil, so a nil pointer of a
concrete error type (typed nil) was still wrapped. Calling Error() on
that pointer then panicked.

Use reflection to detect nilable kinds and return nil for typed nils.

Fixes go-errors#35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nil check when wrapping errors fails for custom error types

1 participant