Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch panics when calling GoString like fmt %#v does (#1) #87

Merged
merged 1 commit into from Aug 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
_go*
_test*
_obj
/.idea
19 changes: 19 additions & 0 deletions formatter.go
Expand Up @@ -92,6 +92,24 @@ type visit struct {
typ reflect.Type
}

func (p *printer) catchPanic(v reflect.Value, method string) {
if r := recover(); r != nil {
if v.Kind() == reflect.Ptr && v.IsNil() {
writeByte(p, '(')
io.WriteString(p, v.Type().String())
io.WriteString(p, ")(nil)")
return
}
writeByte(p, '(')
io.WriteString(p, v.Type().String())
io.WriteString(p, ")(PANIC=calling method ")
io.WriteString(p, strconv.Quote(method))
io.WriteString(p, ": ")
fmt.Fprint(p, r)
writeByte(p, ')')
}
}

func (p *printer) printValue(v reflect.Value, showType, quote bool) {
if p.depth > 10 {
io.WriteString(p, "!%v(DEPTH EXCEEDED)")
Expand All @@ -101,6 +119,7 @@ func (p *printer) printValue(v reflect.Value, showType, quote bool) {
if v.IsValid() && v.CanInterface() {
i := v.Interface()
if goStringer, ok := i.(fmt.GoStringer); ok {
defer p.catchPanic(v, "GoString")
io.WriteString(p, goStringer.GoString())
return
}
Expand Down
38 changes: 37 additions & 1 deletion formatter_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"strings"
"testing"
"time"
"unsafe"
)

Expand Down Expand Up @@ -97,7 +98,7 @@ var gosyntax = []test{
`[]string{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"}`,
},
{F(5), "pretty.F(5)"},
{ NewStructWithPrivateFields("foo"), `NewStructWithPrivateFields("foo")`},
{NewStructWithPrivateFields("foo"), `NewStructWithPrivateFields("foo")`},
{
SA{&T{1, 2}, T{3, 4}},
`pretty.SA{
Expand Down Expand Up @@ -176,6 +177,41 @@ var gosyntax = []test{
},
}`,
},
{(*time.Time)(nil), "(*time.Time)(nil)"},
{&ValueGoString{"vgs"}, `VGS vgs`},
{(*ValueGoString)(nil), `(*pretty.ValueGoString)(nil)`},
{(*VGSWrapper)(nil), `(*pretty.VGSWrapper)(nil)`},
{&PointerGoString{"pgs"}, `PGS pgs`},
{(*PointerGoString)(nil), "(*pretty.PointerGoString)(nil)"},
{&PanicGoString{"oops!"}, `(*pretty.PanicGoString)(PANIC=calling method "GoString": oops!)`},
}

type ValueGoString struct {
s string
}

func (g ValueGoString) GoString() string {
return "VGS " + g.s
}

type VGSWrapper struct {
ValueGoString
}

type PointerGoString struct {
s string
}

func (g *PointerGoString) GoString() string {
return "PGS " + g.s
}

type PanicGoString struct {
s string
}

func (g *PanicGoString) GoString() string {
panic(g.s)
}

func TestGoSyntax(t *testing.T) {
Expand Down