-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherr_test.go
More file actions
52 lines (40 loc) · 1.03 KB
/
err_test.go
File metadata and controls
52 lines (40 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package errtrace
import (
"errors"
"strings"
"testing"
)
func ErrAtDepth(n int) error {
if n == 0 {
return Wrap(errors.New("test error"))
}
return WrapVars(ErrAtDepth(n - 1), map[string]any{"remaining iters": n})
}
func TestMessage(t *testing.T) {
err := ErrAtDepth(1)
msg := err.Error()
if !strings.Contains(msg, "go-errtrace/err_test.go:") {
t.Errorf("expected error text, got: %s", msg)
}
}
func TestStackTrace(t *testing.T) {
err := ErrAtDepth(3)
var tracedErr *TracedError
if !errors.As(err, &tracedErr) {
t.Errorf("expected error to be of type *TracedError, got: %T", err)
}
for i := range 3 {
file := tracedErr.stacktrace[i].file
if !strings.Contains(file, "go-errtrace/err_test.go") {
t.Errorf("expected stack trace to contain 'go-errtrace/err_test.go, got: %s'", file)
}
}
}
func TestFormatter(t *testing.T) {
err := ErrAtDepth(3)
var tracedErr *TracedError
if !errors.As(err, &tracedErr) {
t.Errorf("expected error to be of type *TracedError, got: %T", err)
}
panic(tracedErr.ErrorFormated())
}