]> Cypherpunks repositories - gostls13.git/commitdiff
fmt: include failing method name in panic message
authorIan Lance Taylor <iant@golang.org>
Wed, 12 Dec 2018 23:46:20 +0000 (15:46 -0800)
committerIan Lance Taylor <iant@golang.org>
Thu, 13 Dec 2018 22:41:52 +0000 (22:41 +0000)
Fixes #25707

Change-Id: Idfa379db8cc0e105ea68455ec0b4a0dbc1b3f485
Reviewed-on: https://go-review.googlesource.com/c/153827
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Rob Pike <r@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>

src/fmt/fmt_test.go
src/fmt/print.go

index 1907268c748bbab2ebb70405f5da8b657c64e267..068c2620a8a20955f053fad109261ab4e197533a 100644 (file)
@@ -1683,18 +1683,18 @@ var panictests = []struct {
 }{
        // String
        {"%s", (*PanicS)(nil), "<nil>"}, // nil pointer special case
-       {"%s", PanicS{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
-       {"%s", PanicS{3}, "%!s(PANIC=3)"},
+       {"%s", PanicS{io.ErrUnexpectedEOF}, "%!s(PANIC=String method: unexpected EOF)"},
+       {"%s", PanicS{3}, "%!s(PANIC=String method: 3)"},
        // GoString
        {"%#v", (*PanicGo)(nil), "<nil>"}, // nil pointer special case
-       {"%#v", PanicGo{io.ErrUnexpectedEOF}, "%!v(PANIC=unexpected EOF)"},
-       {"%#v", PanicGo{3}, "%!v(PANIC=3)"},
+       {"%#v", PanicGo{io.ErrUnexpectedEOF}, "%!v(PANIC=GoString method: unexpected EOF)"},
+       {"%#v", PanicGo{3}, "%!v(PANIC=GoString method: 3)"},
        // Issue 18282. catchPanic should not clear fmtFlags permanently.
-       {"%#v", []interface{}{PanicGo{3}, PanicGo{3}}, "[]interface {}{%!v(PANIC=3), %!v(PANIC=3)}"},
+       {"%#v", []interface{}{PanicGo{3}, PanicGo{3}}, "[]interface {}{%!v(PANIC=GoString method: 3), %!v(PANIC=GoString method: 3)}"},
        // Format
        {"%s", (*PanicF)(nil), "<nil>"}, // nil pointer special case
-       {"%s", PanicF{io.ErrUnexpectedEOF}, "%!s(PANIC=unexpected EOF)"},
-       {"%s", PanicF{3}, "%!s(PANIC=3)"},
+       {"%s", PanicF{io.ErrUnexpectedEOF}, "%!s(PANIC=Format method: unexpected EOF)"},
+       {"%s", PanicF{3}, "%!s(PANIC=Format method: 3)"},
 }
 
 func TestPanics(t *testing.T) {
index 5df34a25e532044b6eb50068fea41ef89e662ab3..42fcd8b979b01e5f1cce657085621c7546636da7 100644 (file)
@@ -538,7 +538,7 @@ func (p *pp) fmtPointer(value reflect.Value, verb rune) {
        }
 }
 
-func (p *pp) catchPanic(arg interface{}, verb rune) {
+func (p *pp) catchPanic(arg interface{}, verb rune, method string) {
        if err := recover(); err != nil {
                // If it's a nil pointer, just say "<nil>". The likeliest causes are a
                // Stringer that fails to guard against nil or a nil pointer for a
@@ -561,6 +561,8 @@ func (p *pp) catchPanic(arg interface{}, verb rune) {
                p.buf.WriteString(percentBangString)
                p.buf.WriteRune(verb)
                p.buf.WriteString(panicString)
+               p.buf.WriteString(method)
+               p.buf.WriteString(" method: ")
                p.panicking = true
                p.printArg(err, 'v')
                p.panicking = false
@@ -577,7 +579,7 @@ func (p *pp) handleMethods(verb rune) (handled bool) {
        // Is it a Formatter?
        if formatter, ok := p.arg.(Formatter); ok {
                handled = true
-               defer p.catchPanic(p.arg, verb)
+               defer p.catchPanic(p.arg, verb, "Format")
                formatter.Format(p, verb)
                return
        }
@@ -586,7 +588,7 @@ func (p *pp) handleMethods(verb rune) (handled bool) {
        if p.fmt.sharpV {
                if stringer, ok := p.arg.(GoStringer); ok {
                        handled = true
-                       defer p.catchPanic(p.arg, verb)
+                       defer p.catchPanic(p.arg, verb, "GoString")
                        // Print the result of GoString unadorned.
                        p.fmt.fmtS(stringer.GoString())
                        return
@@ -604,13 +606,13 @@ func (p *pp) handleMethods(verb rune) (handled bool) {
                        switch v := p.arg.(type) {
                        case error:
                                handled = true
-                               defer p.catchPanic(p.arg, verb)
+                               defer p.catchPanic(p.arg, verb, "Error")
                                p.fmtString(v.Error(), verb)
                                return
 
                        case Stringer:
                                handled = true
-                               defer p.catchPanic(p.arg, verb)
+                               defer p.catchPanic(p.arg, verb, "String")
                                p.fmtString(v.String(), verb)
                                return
                        }