davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
5.98k stars 361 forks source link

spew.Dump panics when printing a custom error that wraps another custom error #144

Open Justinfan827 opened 11 months ago

Justinfan827 commented 11 months ago
import (
    "fmt"
    "testing"

    "github.com/davecgh/go-spew/spew"
    "github.com/stretchr/testify/assert"
)

type NestedErr struct {
    Err error
}

func (e *NestedErr) Error() string {
    return e.Err.Error()
}

type ErrWrap struct {
    NestedErr *NestedErr
}

func TestIfPanic(t *testing.T) {
    its := assert.New(t)
    err := ErrWrap{
        NestedErr: &NestedErr{
            Err: fmt.Errorf("this is an error"),
        },
    }
    its.NotPanics(func() {
        ss := spew.Sdump(err)
        fmt.Println("LOGGING", ss)

    })
}

go version go1.20.3 darwin/arm64 spew version: github.com/davecgh/go-spew v1.1.1

Note that this does not panic if I try to dump a pointer to ErrWrap e.g.

    err := &ErrWrap{
        NestedErr: &NestedErr{
            Err: fmt.Errorf("this is an error"),
        },
    }