-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors_test.go
More file actions
142 lines (120 loc) · 2.82 KB
/
errors_test.go
File metadata and controls
142 lines (120 loc) · 2.82 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 (
"io"
"os"
"testing"
)
func TestNew(t *testing.T) {
err := New("hello %v", "world")
t.Logf("failed: %+v", err)
err = Skip(1).
WithSkip(0).
WithMessage("bug skip 0").
Build()
t.Logf("failed: %+v", err)
err = Message("1").
WithSkip(0).
WithMessage("bug msg").
Build()
t.Logf("failed: %+v", err)
err = NewBuilder().
WithCode(Internal).
WithErrors(io.EOF).
WithErrors(io.ErrShortWrite).
Build()
t.Logf("failed: %+v", err)
err = NewBuilder().
WithCode(Internal).
WithErrors(io.EOF).
WithErrors(io.ErrShortWrite).
Build()
// Attach(io.ErrClosedPipe)
t.Logf("failed: %+v", err)
err = NewBuilder().Build()
err.Attach(os.ErrClosed, os.ErrInvalid, os.ErrPermission)
t.Logf("failed: %+v", err)
err = New(WithErrors(io.EOF, io.ErrShortWrite))
t.Logf("failed: %+v", err)
err = New()
t.Logf("failed: %+v", err)
// since v3.0.5, Attach() has no return value
// err = New("hello").Attach(io.EOF)
// t.Logf("failed: %+v", err)
}
func TestUnwrap(t *testing.T) {
t.Log("unwrap all inner error (including Code object) one by one:")
err := New()
err. // WithCode(NotFound).
WithErrors(io.EOF, io.ErrShortBuffer).
WithMessage("has code and errors").
Attach(os.ErrClosed, os.ErrInvalid, os.ErrPermission)
var e error = err
for e != nil {
e = Unwrap(err)
t.Logf("failed: %v", e)
}
if o, ok := err.(interface{ Reset() }); ok {
o.Reset()
}
t.Log("again")
e = err
for e != nil {
e = Unwrap(err)
t.Logf("failed: %v", e)
}
}
func TestWithStackInfo_New(t *testing.T) {
err := New("hello %v", "world")
err.WithErrors(io.EOF, io.ErrShortWrite).
WithErrors(io.ErrClosedPipe).
WithCode(Internal).
End()
t.Logf("failed: %+v", err)
if CanCause(err) {
e := err.(causer).Cause()
t.Logf("failed: %v", e)
}
if CanCauses(err) {
e := Causes(err)
t.Logf("failed: %v", e)
}
err2 := New("hello %v", "world")
err2.WithData(9, err, 10).WithTaggedData(TaggedData{"9": 9, "10": 10}).End()
t.Logf("failed: %+v", err2)
t.Logf("Data: %v", err2.Data())
t.Logf("TaggedData: %v", err2.TaggedData())
}
func TestTemplateFormat(t *testing.T) {
err := New("cannot set: %v (%v) -> %v (%v)")
_ = err.FormatWith("a", "b", "c", "d")
t.Logf("Error: %v", err)
t.Logf("Error: %+v", err)
_ = err.FormatWith("1", "2", "3", "4")
t.Logf("Error: %v", err)
}
func TestContainerMore(t *testing.T) {
var err error
ec := New("copying got errors")
ec.Attach(New("some error"))
ec.Defer(&err)
if err == nil {
t.Fatal(`bad`)
} else {
t.Logf(`wanted err is non-nil: %v`, err)
}
}
func TestIsDeep(t *testing.T) {
var err error
ec := New("copying got errors")
in := New("unmatched %q")
ec.Attach(in.FormatWith("demo"))
ec.Defer(&err)
if err == nil {
t.Fatal(`bad`)
} else {
t.Logf(`wanted err is non-nil: %v`, err)
if !Is(err, in) {
t.Fatal("expecting Is() got returning true")
}
}
}