-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwrap_test.go
141 lines (119 loc) · 4.52 KB
/
wrap_test.go
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
package errors_test
import (
"bytes"
"fmt"
"io"
"os"
"strings"
"testing"
"github.com/mailgun/errors"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestWrap(t *testing.T) {
err := &ErrTest{Msg: "query error"}
wrap := errors.Wrap(err, "message")
assert.NotNil(t, wrap)
wrapf := errors.Wrapf(err, "message: %d", 1)
assert.NotNil(t, wrapf)
t.Run("Wrap/Wrapf should return wrap the error", func(t *testing.T) {
assert.Equal(t, "message: query error", wrap.Error())
assert.Equal(t, "message: 1: query error", wrapf.Error())
})
t.Run("Unwrap should return ErrTest", func(t *testing.T) {
u := errors.Unwrap(wrap)
require.NotNil(t, u)
assert.Equal(t, "query error", u.Error())
uf := errors.Unwrap(wrapf)
require.NotNil(t, uf)
assert.Equal(t, "query error", uf.Error())
})
t.Run("Extract stack fields as a normal map", func(t *testing.T) {
mf := errors.ToMap(wrapf)
assert.Len(t, mf, 5)
m := errors.ToMap(wrap)
require.NotNil(t, m)
assert.Regexp(t, ".*/wrap_test.go", m["excFileName"])
assert.Regexp(t, "\\d*", m["excLineNum"])
assert.Equal(t, "message: query error", m["excValue"])
assert.Equal(t, "errors_test.TestWrap", m["excFuncName"])
assert.Equal(t, "*errors_test.ErrTest", m["excType"])
assert.Len(t, m, 5)
})
t.Run("Can use errors.Is() from std `errors` package", func(t *testing.T) {
assert.True(t, errors.Is(err, &ErrTest{}))
assert.True(t, errors.Is(wrap, &ErrTest{}))
assert.True(t, errors.Is(wrapf, &ErrTest{}))
})
t.Run("Can use errors.As() from std `errors` package", func(t *testing.T) {
myErr := &ErrTest{}
assert.True(t, errors.As(wrap, &myErr))
assert.Equal(t, myErr.Msg, "query error")
assert.True(t, errors.As(wrapf, &myErr))
assert.Equal(t, myErr.Msg, "query error")
})
t.Run("Extract as Logrus fields", func(t *testing.T) {
ff := errors.ToLogrus(wrapf)
assert.Len(t, ff, 5)
f := errors.ToLogrus(wrap)
require.NotNil(t, f)
b := bytes.Buffer{}
logrus.SetOutput(&b)
logrus.WithFields(f).Info("test logrus fields")
logrus.SetOutput(os.Stdout)
assert.Contains(t, b.String(), "test logrus fields")
assert.Contains(t, b.String(), `excValue="message: query error"`)
assert.Contains(t, b.String(), `excType="*errors_test.ErrTest"`)
assert.Contains(t, b.String(), "excFuncName=errors_test.TestWrap")
assert.Regexp(t, "excFileName=.*/wrap_test.go", b.String())
assert.Regexp(t, "excLineNum=\\d*", b.String())
// OUTPUT: time="2023-01-26T10:37:48-05:00" level=info msg="test logrus fields"
// excFileName=errors/fields_test.go excFuncName=errors_test.TestWithFields
// excLineNum=18 excType="*errors_test.ErrTest" excValue="message: query error" key1=value1
t.Log(b.String())
assert.Equal(t, "message: query error", wrap.Error())
out := fmt.Sprintf("%+v", wrap)
assert.True(t, strings.Contains(out, `message: query error`))
})
t.Run("Wrap() should return nil, if error is nil", func(t *testing.T) {
assert.Nil(t, errors.Wrap(nil, "no error"))
})
t.Run("Wrapf() should return nil, if error is nil", func(t *testing.T) {
assert.Nil(t, errors.Wrapf(nil, "no '%d' error", 1))
})
}
func TestWrapFmtDirectives(t *testing.T) {
t.Run("Wrapf()", func(t *testing.T) {
err := errors.Wrapf(errors.New("error"), "shit happened '%d'", 1)
assert.Equal(t, "shit happened '1': error", fmt.Sprintf("%s", err))
assert.Equal(t, "shit happened '1': error", fmt.Sprintf("%v", err))
assert.Equal(t, "shit happened '1': error", fmt.Sprintf("%+v", err))
assert.Equal(t, "*errors.wrappedError", fmt.Sprintf("%T", err))
})
t.Run("Wrap()", func(t *testing.T) {
err := errors.Wrapf(errors.New("error"), "")
assert.Equal(t, "error", fmt.Sprintf("%s", err))
assert.Equal(t, "error", fmt.Sprintf("%v", err))
assert.Equal(t, "error", fmt.Sprintf("%+v", err))
assert.Equal(t, "*errors.wrappedError", fmt.Sprintf("%T", err))
})
}
func TestWrapErrorValue(t *testing.T) {
err := io.EOF
wrap := errors.Wrap(err, "message")
assert.True(t, errors.Is(wrap, io.EOF))
}
func TestCause(t *testing.T) {
err := errors.Errorf("error: %w", errors.Wrap(errors.Wrap(errors.New("the cause"), "wrap 2"), "wrap 1"))
cause := errors.Cause(err)
require.Error(t, err)
require.Error(t, cause)
assert.Equal(t, "error: wrap 1: wrap 2: the cause", err.Error())
assert.Equal(t, "the cause", cause.Error())
}
// Ensure errors.wrappedError returns an error that is compatible with `github.com/pkf/errors.Cause()`
func TestWrappedCause(t *testing.T) {
err := errors.Wrap(io.EOF, "message")
assert.Equal(t, io.EOF, pkgErrorCause(err))
}