forked from getlantern/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
142 lines (123 loc) · 4.03 KB
/
errors_test.go
File metadata and controls
142 lines (123 loc) · 4.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package errors
import (
"bytes"
"errors"
"fmt"
"io"
"net"
"regexp"
"testing"
"github.com/getlantern/context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var (
replaceNumbers = regexp.MustCompile(`[0-9]+`)
replaceArch = regexp.MustCompile(`asm_[a-z0-9]+\.s`)
)
func TestNewWithCause(t *testing.T) {
cause := buildCause()
outer := New("Hello %v", cause)
assert.Equal(t, "Hello World", outer.Error())
assert.Equal(t, "Hello %v", outer.ErrorClean())
require.IsType(t, (*wrappingError)(nil), outer, "Including an error arg should have resulted in a *wrappingError")
assert.Equal(t,
"github.com/getlantern/errors.TestNewWithCause (errors_test.go:999)",
replaceNumbers.ReplaceAllString(outer.(*wrappingError).data["error_location"].(string), "999"))
assert.Equal(t, cause, outer.(*wrappingError).wrapped)
// Make sure that stacktrace prints out okay
buf := &bytes.Buffer{}
print := outer.MultiLinePrinter()
for {
more := print(buf)
buf.WriteByte('\n')
if !more {
break
}
}
expected := `Hello World
at github.com/getlantern/errors.TestNewWithCause (errors_test.go:999)
at testing.tRunner (testing.go:999)
at runtime.goexit (asm_arch.s:999)
Caused by: World
at github.com/getlantern/errors.buildCause (errors_test.go:999)
at github.com/getlantern/errors.TestNewWithCause (errors_test.go:999)
at testing.tRunner (testing.go:999)
at runtime.goexit (asm_arch.s:999)
Caused by: orld
Caused by: ld
at github.com/getlantern/errors.buildSubSubCause (errors_test.go:999)
at github.com/getlantern/errors.buildSubCause (errors_test.go:999)
at github.com/getlantern/errors.buildCause (errors_test.go:999)
at github.com/getlantern/errors.TestNewWithCause (errors_test.go:999)
at testing.tRunner (testing.go:999)
at runtime.goexit (asm_arch.s:999)
Caused by: d
`
assert.Equal(t,
expected,
replaceArch.ReplaceAllString(
replaceNumbers.ReplaceAllString(buf.String(), "999"),
"asm_arch.s",
))
assert.Equal(t, buildSubSubSubCause(), outer.RootCause())
}
func buildCause() Error {
return New("W%v", buildSubCause())
}
func buildSubCause() error {
return fmt.Errorf("or%w", buildSubSubCause())
}
func buildSubSubCause() error {
return New("l%v", buildSubSubSubCause())
}
func buildSubSubSubCause() error {
return fmt.Errorf("d")
}
func TestWrapNil(t *testing.T) {
assert.Nil(t, doWrapNil())
}
func doWrapNil() error {
return Wrap(nil)
}
func TestFill(t *testing.T) {
e := New("something happened").(*baseError)
e2 := New("uh oh: %v", e).(*wrappingError)
e3 := fmt.Errorf("hmm: %w", e2)
e4 := New("umm: %v", e3).(*wrappingError)
e4.data["name"] = "e4"
e2.data["name"] = "e2"
e.data["name"] = "e"
e2.data["k"] = "v2"
e.data["k"] = "v"
e.data["a"] = "b"
m := context.Map{}
e4.Fill(m)
require.Equal(t, "e4", m["name"])
require.Equal(t, "v2", m["k"])
require.Equal(t, "b", m["a"])
}
// Ensures that this package implements error unwrapping as described in:
// https://golang.org/pkg/errors/#pkg-overview
func TestUnwrapping(t *testing.T) {
sampleUnwrapper := fmt.Errorf("%w", fmt.Errorf("something happened"))
errNoCause := New("something happened")
_, ok := errNoCause.(unwrapper)
assert.False(t, ok, "error with no cause should not implement Unwrap method")
wrappedNoCause := Wrap(errNoCause)
_, ok = wrappedNoCause.(unwrapper)
assert.False(t, ok, "wrapped error with no cause should not implement Unwrap method")
errFromEOF := New("something happened: %v", io.EOF)
assert.Implements(t, &sampleUnwrapper, errFromEOF)
assert.True(t, errors.Is(errFromEOF, io.EOF))
wrappedFromEOF := Wrap(errFromEOF)
assert.Implements(t, &sampleUnwrapper, wrappedFromEOF)
assert.True(t, errors.Is(wrappedFromEOF, io.EOF))
addrErrHolder := new(net.AddrError)
errFromAddrErr := New("something happend: %v", new(net.AddrError))
assert.Implements(t, &sampleUnwrapper, errFromAddrErr)
assert.True(t, errors.As(errFromAddrErr, &addrErrHolder))
wrappedFromAddrErr := Wrap(errFromAddrErr)
assert.Implements(t, &sampleUnwrapper, wrappedFromAddrErr)
assert.True(t, errors.As(wrappedFromAddrErr, &addrErrHolder))
}