-
Notifications
You must be signed in to change notification settings - Fork 0
/
error_test.go
95 lines (72 loc) · 2.23 KB
/
error_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
package elk
import (
"errors"
"testing"
"github.com/studio-b12/elk/internal/assert"
)
func TestCast(t *testing.T) {
const ErrCode = ErrorCode("some-error-code")
t.Run("error", func(t *testing.T) {
err := errors.New("some error")
wrappedErr := Wrap(ErrCode, err)
castError := Cast(wrappedErr)
assert.Equal(t, wrappedErr, castError)
assert.Equal(t, err, castError.Unwrap())
})
t.Run("error-default", func(t *testing.T) {
err := errors.New("some error")
castError := Cast(err)
assert.Equal(t, castError.Code(), CodeUnexpected)
assert.Equal(t, err, castError.Unwrap())
})
t.Run("error-custom", func(t *testing.T) {
err := errors.New("some error")
castError := Cast(err, ErrCode)
assert.Equal(t, castError.Code(), ErrCode)
assert.Equal(t, err, castError.Unwrap())
})
t.Run("error-join", func(t *testing.T) {
err := errors.Join(nil)
castError := Cast(err, ErrCode)
assert.Equal(t, castError.Code(), ErrCode)
err = errors.Join(errors.New("foo"), errors.New("bar"))
castError = Cast(err, ErrCode)
assert.Equal(t, castError.Code(), ErrCode)
customCode := ErrorCode("custom-code")
customCode2 := ErrorCode("custom-code-2")
err = errors.Join(errors.New("foo"), NewError(customCode))
castError = Cast(err, ErrCode)
assert.Equal(t, castError.Code(), customCode)
err = errors.Join(NewError(customCode), NewError(customCode2))
castError = Cast(err, ErrCode)
assert.Equal(t, castError.Code(), ErrCode)
})
t.Run("custom-model", func(t *testing.T) {
errCode := ErrorCode("custom-code")
type MyError struct {
InnerError
SomeData string
}
err := MyError{
InnerError: InnerError{Inner: NewError(errCode, "some message")},
SomeData: "some data",
}
castErrCode := Cast(err).Code()
assert.Equal(t, CodeUnexpected, castErrCode)
})
t.Run("custom-model-wrapped", func(t *testing.T) {
errCode := ErrorCode("custom-code")
wrappedErrCode := ErrorCode("wrapped-custom-code")
type MyError struct {
InnerError
SomeData string
}
wrappedErr := NewError(wrappedErrCode, "some message")
err := MyError{
InnerError: InnerError{Inner: Wrap(errCode, wrappedErr)},
SomeData: "some data",
}
castErrCode := Cast(err).Code()
assert.Equal(t, CodeUnexpected, castErrCode)
})
}