@@ -4,8 +4,11 @@ import (
4
4
"bytes"
5
5
"fmt"
6
6
"io"
7
+ "runtime"
7
8
"strings"
8
9
"testing"
10
+
11
+ "github.com/pkg/errors"
9
12
)
10
13
11
14
// fixture functions doing work to avoid inlining
@@ -44,7 +47,7 @@ func TestParsePanicStack(t *testing.T) {
44
47
}
45
48
expected := []StackFrame {
46
49
StackFrame {Name : "TestParsePanicStack.func1" , File : "errors/error_test.go" },
47
- StackFrame {Name : "a" , File : "errors/error_test.go" , LineNumber : 13 },
50
+ StackFrame {Name : "a" , File : "errors/error_test.go" , LineNumber : 16 },
48
51
}
49
52
assertStacksMatch (t , expected , err .StackFrames ())
50
53
}()
@@ -88,7 +91,7 @@ func TestSkipWorks(t *testing.T) {
88
91
}
89
92
90
93
expected := []StackFrame {
91
- StackFrame {Name : "a" , File : "errors/error_test.go" , LineNumber : 13 },
94
+ StackFrame {Name : "a" , File : "errors/error_test.go" , LineNumber : 16 },
92
95
}
93
96
94
97
assertStacksMatch (t , expected , err .StackFrames ())
@@ -182,6 +185,26 @@ func TestNewError(t *testing.T) {
182
185
}
183
186
}
184
187
188
+ func TestUnwrapPkgError (t * testing.T ) {
189
+ _ , _ , line , ok := runtime .Caller (0 ) // grab line immediately before error generator
190
+ top := func () error {
191
+ err := fmt .Errorf ("OH NO" )
192
+ return errors .Wrap (err , "failed" ) // the correct line for the top of the stack
193
+ }
194
+ unwrapped := New (top (), 0 ) // if errors.StackTrace detection fails, this line will be top of stack
195
+ if ! ok {
196
+ t .Fatalf ("Something has gone wrong with loading the current stack" )
197
+ }
198
+ if unwrapped .Error () != "failed: OH NO" {
199
+ t .Errorf ("Failed to unwrap error: %s" , unwrapped .Error ())
200
+ }
201
+ expected := []StackFrame {
202
+ StackFrame {Name : "TestUnwrapPkgError.func1" , File : "errors/error_test.go" , LineNumber : line + 3 },
203
+ StackFrame {Name : "TestUnwrapPkgError" , File : "errors/error_test.go" , LineNumber : line + 5 },
204
+ }
205
+ assertStacksMatch (t , expected , unwrapped .StackFrames ())
206
+ }
207
+
185
208
func ExampleErrorf () {
186
209
for i := 1 ; i <= 2 ; i ++ {
187
210
if i % 2 == 1 {
0 commit comments