瀏覽代碼

Wrapper errors now print full stacktrace (#57)

Dave Cheney 9 年之前
父節點
當前提交
0bc61eb85b
共有 2 個文件被更改,包括 12 次插入3 次删除
  1. 2 1
      errors.go
  2. 10 2
      format_test.go

+ 2 - 1
errors.go

@@ -145,7 +145,8 @@ func (w wrapper) Format(s fmt.State, verb rune) {
 	case 'v':
 		if s.Flag('+') {
 			fmt.Fprintf(s, "%+v\n", w.Cause())
-			fmt.Fprintf(s, "%+v: %s", w.StackTrace()[0], w.msg)
+			io.WriteString(s, w.msg)
+			fmt.Fprintf(s, "%+v", w.StackTrace())
 			return
 		}
 		fallthrough

+ 10 - 2
format_test.go

@@ -107,8 +107,9 @@ func TestFormatWrapf(t *testing.T) {
 		Wrap(io.EOF, "error"),
 		"%+v",
 		"EOF\n" +
+			"error\n" +
 			"github.com/pkg/errors.TestFormatWrapf\n" +
-			"\t.+/github.com/pkg/errors/format_test.go:107: error",
+			"\t.+/github.com/pkg/errors/format_test.go:107",
 	}, {
 		Wrapf(New("error"), "error%d", 2),
 		"%v",
@@ -118,7 +119,14 @@ func TestFormatWrapf(t *testing.T) {
 		"%+v",
 		"error\n" +
 			"github.com/pkg/errors.TestFormatWrapf\n" +
-			"\t.+/github.com/pkg/errors/format_test.go:117",
+			"\t.+/github.com/pkg/errors/format_test.go:118",
+	}, {
+		Wrap(Wrap(io.EOF, "error1"), "error2"),
+		"%+v",
+		"EOF\n" +
+			"error1\n" +
+			"github.com/pkg/errors.TestFormatWrapf\n" +
+			"\t.+/github.com/pkg/errors/format_test.go:124\n",
 	}}
 
 	for _, tt := range tests {