-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathinterception_error_internal_test.go
More file actions
133 lines (120 loc) · 4.03 KB
/
Copy pathinterception_error_internal_test.go
File metadata and controls
133 lines (120 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
package aibridge
import (
"context"
"strings"
"testing"
"unicode/utf8"
"github.com/stretchr/testify/assert"
"golang.org/x/xerrors"
"github.com/coder/coder/v2/aibridge/circuitbreaker"
"github.com/coder/coder/v2/aibridge/keypool"
"github.com/coder/coder/v2/aibridge/recorder"
)
// stubCategorizer is a test errorCategorizer standing in for a provider.
type stubCategorizer struct {
result *recorder.ErrorType
}
func (s stubCategorizer) CategorizeError(error) *recorder.ErrorType {
return s.result
}
func ptr(t recorder.ErrorType) *recorder.ErrorType { return &t }
func TestCategorizeInterceptionError(t *testing.T) {
t.Parallel()
cases := []struct {
name string
cat stubCategorizer
err error
wantType recorder.ErrorType
wantMsg string
}{
{
name: "nil success",
err: nil,
wantType: "",
wantMsg: "",
},
{
name: "circuit open maps to server error",
err: circuitbreaker.ErrCircuitOpen,
wantType: recorder.ErrorTypeServerError,
wantMsg: circuitbreaker.ErrCircuitOpen.Error(),
},
{
name: "context deadline is timeout",
err: context.DeadlineExceeded,
wantType: recorder.ErrorTypeTimeout,
wantMsg: context.DeadlineExceeded.Error(),
},
{
name: "keypool permanent is unauthorized",
err: &keypool.Error{Kind: keypool.ErrorKindPermanent},
wantType: recorder.ErrorTypeUnauthorized,
wantMsg: (&keypool.Error{Kind: keypool.ErrorKindPermanent}).Error(),
},
{
name: "keypool unauthorized is unauthorized",
err: &keypool.Error{Kind: keypool.ErrorKindUnauthorized},
wantType: recorder.ErrorTypeUnauthorized,
wantMsg: (&keypool.Error{Kind: keypool.ErrorKindUnauthorized}).Error(),
},
{
name: "keypool rate limited is rate limited",
err: &keypool.Error{Kind: keypool.ErrorKindRateLimited},
wantType: recorder.ErrorTypeRateLimited,
wantMsg: (&keypool.Error{Kind: keypool.ErrorKindRateLimited}).Error(),
},
{
name: "keypool unrecognized kind is unknown",
err: &keypool.Error{Kind: keypool.ErrorKind(-1)},
wantType: recorder.ErrorTypeUnknown,
wantMsg: (&keypool.Error{Kind: keypool.ErrorKind(-1)}).Error(),
},
{
name: "context canceled is unknown",
err: context.Canceled,
wantType: recorder.ErrorTypeUnknown,
wantMsg: context.Canceled.Error(),
},
{
name: "wrapped keypool error is unwrapped",
err: xerrors.Errorf("key pool exhausted: %w", &keypool.Error{Kind: keypool.ErrorKindPermanent}),
wantType: recorder.ErrorTypeUnauthorized,
wantMsg: "key pool exhausted: all configured keys are permanently unavailable",
},
{
name: "delegated to provider",
cat: stubCategorizer{result: ptr(recorder.ErrorTypeOverloaded)},
err: xerrors.New("provider error"),
wantType: recorder.ErrorTypeOverloaded,
wantMsg: "provider error",
},
{
name: "provider does not recognize the error",
err: xerrors.New("mystery"),
wantType: recorder.ErrorTypeUnknown,
wantMsg: "mystery",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
gotType, gotMsg := categorizeInterceptionError(tc.cat, tc.err)
assert.Equal(t, tc.wantType, gotType)
assert.Equal(t, tc.wantMsg, gotMsg)
})
}
}
func TestCategorizeInterceptionErrorTruncatesMessage(t *testing.T) {
t.Parallel()
// ASCII: truncated exactly at the byte cap.
ascii := strings.Repeat("a", maxRecordedErrorMessageBytes*2)
_, gotMsg := categorizeInterceptionError(stubCategorizer{}, xerrors.New(ascii))
assert.Len(t, gotMsg, maxRecordedErrorMessageBytes)
// Multi-byte: the '€' rune (3 bytes) split at the cap is dropped, leaving
// valid UTF-8 just below the cap rather than an invalid trailing fragment.
multibyte := strings.Repeat("€", maxRecordedErrorMessageBytes)
_, gotMsg = categorizeInterceptionError(stubCategorizer{}, xerrors.New(multibyte))
assert.True(t, utf8.ValidString(gotMsg), "truncated message must stay valid UTF-8")
assert.Less(t, len(gotMsg), maxRecordedErrorMessageBytes)
assert.Positive(t, len(gotMsg))
}