Repository navigation
Expand file tree
/
Copy patherrors_test.go
More file actions
78 lines (60 loc) · 2.05 KB
/
Copy patherrors_test.go
File metadata and controls
78 lines (60 loc) · 2.05 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
package workflow
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
func TestPermanent(t *testing.T) {
t.Run("nil in nil out", func(t *testing.T) {
require.NoError(t, Permanent(nil))
})
t.Run("matches via errors.AsType and unwraps to the cause", func(t *testing.T) {
cause := errors.New("boom")
err := Permanent(cause)
permErr, ok := errors.AsType[*PermanentError](err)
require.True(t, ok)
require.ErrorIs(t, permErr.Unwrap(), cause)
})
t.Run("matches through a wrapped error", func(t *testing.T) {
cause := errors.New("boom")
wrapped := fmt.Errorf("context: %w", Permanent(cause))
permErr, ok := errors.AsType[*PermanentError](wrapped)
require.True(t, ok)
require.ErrorIs(t, permErr.Unwrap(), cause)
})
}
func TestWithCode(t *testing.T) {
cause := errors.New("boom")
t.Run("nil in nil out", func(t *testing.T) {
require.NoError(t, WithCode("rejected", nil))
})
t.Run("keeps the cause matchable", func(t *testing.T) {
require.ErrorIs(t, WithCode("rejected", cause), cause)
})
t.Run("codeOf reads the code back", func(t *testing.T) {
require.Equal(t, "rejected", codeOf(WithCode("rejected", cause)))
})
t.Run("codeOf is empty for an untagged error", func(t *testing.T) {
require.Empty(t, codeOf(cause))
require.Empty(t, codeOf(Permanent(cause)))
})
t.Run("codeOf reads through a wrapped error", func(t *testing.T) {
wrapped := fmt.Errorf("context: %w", WithCode("rejected", cause))
require.Equal(t, "rejected", codeOf(wrapped))
})
t.Run("composes with Permanent wrapping WithCode", func(t *testing.T) {
err := Permanent(WithCode("rejected", cause))
require.Equal(t, "rejected", codeOf(err))
_, permanent := errors.AsType[*PermanentError](err)
require.True(t, permanent)
require.ErrorIs(t, err, cause)
})
t.Run("composes with WithCode wrapping Permanent", func(t *testing.T) {
err := WithCode("rejected", Permanent(cause))
require.Equal(t, "rejected", codeOf(err))
_, permanent := errors.AsType[*PermanentError](err)
require.True(t, permanent)
require.ErrorIs(t, err, cause)
})
}