Skip to content

Commit

Permalink
fix: handle nil error with concrete type
Browse files Browse the repository at this point in the history
  • Loading branch information
pierrre committed Jan 4, 2024
1 parent 0ffa373 commit d446449
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 0 deletions.
10 changes: 10 additions & 0 deletions _assertauto/TestConfig/ErrorNil.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"entries": [
{
"equal": "(*pretty_test.testError) => <nil>"
},
{
"equal": 0
}
]
}
3 changes: 3 additions & 0 deletions ext/pierrreerrors/pierrreerrors.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ func write(c *pretty.Config, w io.Writer, st *pretty.State, v reflect.Value) boo
if !v.Type().Implements(typeError) {
return false
}
if v.Kind() == reflect.Pointer && v.IsNil() {
return false
}
err := v.Interface().(error) //nolint:forcetypeassert // Checked above.
st.Indent++
iw := pretty.GetIndentWriter(w, c, st, true)
Expand Down
15 changes: 15 additions & 0 deletions ext/pierrreerrors/pierrreerrors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ func TestValueWriter(t *testing.T) {
assert.StringHasPrefix(t, s, "(*errstack.stack) error\n\tstack\n")
}

func TestValueWriterNil(t *testing.T) {
c := pretty.NewConfig()
c.ValueWriters = nil
Configure(c)
var err error = (*testError)(nil)
s := c.String(err)
assert.Equal(t, s, "(*pierrreerrors.testError) => <nil>")
}

func TestValueWriterUnexported(t *testing.T) {
c := pretty.NewConfig()
c.ValueWriters = nil
Expand All @@ -27,6 +36,12 @@ func TestValueWriterUnexported(t *testing.T) {
assert.StringHasPrefix(t, s, "(*pierrreerrors.testUnexported) => (pierrreerrors.testUnexported) {\n\tv: (*errstack.stack) => (errstack.stack) {\n\t\terror: (*errors.errorString) => (errors.errorString)")
}

type testError struct{}

func (e *testError) Error() string {
return "test"
}

type testUnexported struct {
v any
}
3 changes: 3 additions & 0 deletions pretty.go
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,9 @@ func writeError(c *Config, w io.Writer, st *State, v reflect.Value) bool {
if !v.Type().Implements(typeError) {
return false
}
if v.Kind() == reflect.Pointer && v.IsNil() {
return false
}
err := v.Interface().(error) //nolint:forcetypeassert // Checked above.
_, _ = writeString(w, "=> .Error() => ")
_, _ = strconvio.WriteQuote(w, err.Error())
Expand Down
7 changes: 7 additions & 0 deletions pretty_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,13 @@ var testCases = []testCase{
c.ValueWriters = []ValueWriter{NewErrorValueWriter()}
},
},
{
name: "ErrorNil",
value: (*testError)(nil),
configure: func(c *Config) {
c.ValueWriters = []ValueWriter{NewErrorValueWriter()}
},
},
{
name: "ErrorUnexported",
value: testUnexported{v: &testError{}},
Expand Down

0 comments on commit d446449

Please sign in to comment.