-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors_test.go
More file actions
186 lines (169 loc) · 4.87 KB
/
errors_test.go
File metadata and controls
186 lines (169 loc) · 4.87 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package threads
import (
"errors"
"fmt"
"strings"
"testing"
"time"
)
func TestNewNetworkError(t *testing.T) {
err := NewNetworkError(0, "connection refused", "details", true)
if err == nil {
t.Fatal("Expected non-nil error")
}
if err.Code != 0 {
t.Errorf("Expected code 0, got %d", err.Code)
}
if err.Message != "connection refused" {
t.Errorf("Expected message 'connection refused', got %q", err.Message)
}
if !err.Temporary {
t.Error("Expected Temporary to be true")
}
if err.Cause != nil {
t.Error("Expected Cause to be nil for NewNetworkError")
}
}
func TestNewNetworkError_NonTemporary(t *testing.T) {
err := NewNetworkError(0, "DNS failure", "could not resolve", false)
if err.Temporary {
t.Error("Expected Temporary to be false")
}
}
func TestIsNetworkError(t *testing.T) {
t.Run("returns true for NetworkError", func(t *testing.T) {
err := NewNetworkError(0, "timeout", "details", true)
if !IsNetworkError(err) {
t.Error("Expected IsNetworkError to return true")
}
})
t.Run("returns true for wrapped NetworkError", func(t *testing.T) {
netErr := NewNetworkError(0, "timeout", "details", true)
wrapped := fmt.Errorf("wrapper: %w", netErr)
if !IsNetworkError(wrapped) {
t.Error("Expected IsNetworkError to return true for wrapped NetworkError")
}
})
t.Run("returns false for non-NetworkError", func(t *testing.T) {
err := NewAPIError(500, "server error", "details", "req-123")
if IsNetworkError(err) {
t.Error("Expected IsNetworkError to return false for APIError")
}
})
t.Run("returns false for nil", func(t *testing.T) {
if IsNetworkError(nil) {
t.Error("Expected IsNetworkError to return false for nil")
}
})
}
func TestNewRateLimitError(t *testing.T) {
retryAfter := 30 * time.Second
err := NewRateLimitError(429, "too many requests", "rate limited", retryAfter)
if err == nil {
t.Fatal("Expected non-nil error")
}
if err.Code != 429 {
t.Errorf("Expected code 429, got %d", err.Code)
}
if err.Message != "too many requests" {
t.Errorf("Expected message 'too many requests', got %q", err.Message)
}
if err.RetryAfter != retryAfter {
t.Errorf("Expected RetryAfter %v, got %v", retryAfter, err.RetryAfter)
}
if err.Type != "rate_limit_error" {
t.Errorf("Expected type 'rate_limit_error', got %q", err.Type)
}
}
func TestBaseErrorError(t *testing.T) {
t.Run("with details", func(t *testing.T) {
err := &BaseError{
Code: 400,
Message: "Bad request",
Type: "validation_error",
Details: "field X is invalid",
}
errStr := err.Error()
if errStr == "" {
t.Fatal("Expected non-empty error string")
}
// Should contain details
if !strings.Contains(errStr, "field X is invalid") {
t.Errorf("Expected error string to contain details, got: %s", errStr)
}
})
t.Run("without details", func(t *testing.T) {
err := &BaseError{
Code: 400,
Message: "Bad request",
Type: "validation_error",
}
errStr := err.Error()
if errStr == "" {
t.Fatal("Expected non-empty error string")
}
// Should not contain " - " separator since there are no details
if strings.Contains(errStr, " - ") {
t.Errorf("Expected error string without details separator, got: %s", errStr)
}
})
}
func TestExtractBaseError(t *testing.T) {
t.Run("AuthenticationError", func(t *testing.T) {
err := NewAuthenticationError(401, "unauthorized", "details")
base := extractBaseError(err)
if base == nil {
t.Fatal("Expected non-nil BaseError")
}
if base.Code != 401 {
t.Errorf("Expected code 401, got %d", base.Code)
}
})
t.Run("RateLimitError", func(t *testing.T) {
err := NewRateLimitError(429, "rate limited", "details", time.Minute)
base := extractBaseError(err)
if base == nil {
t.Fatal("Expected non-nil BaseError")
}
if base.Code != 429 {
t.Errorf("Expected code 429, got %d", base.Code)
}
})
t.Run("ValidationError", func(t *testing.T) {
err := NewValidationError(400, "invalid", "details", "field")
base := extractBaseError(err)
if base == nil {
t.Fatal("Expected non-nil BaseError")
}
if base.Code != 400 {
t.Errorf("Expected code 400, got %d", base.Code)
}
})
t.Run("NetworkError", func(t *testing.T) {
err := NewNetworkError(0, "timeout", "details", true)
base := extractBaseError(err)
if base == nil {
t.Fatal("Expected non-nil BaseError")
}
if base.Type != "network_error" {
t.Errorf("Expected type 'network_error', got %q", base.Type)
}
})
t.Run("APIError", func(t *testing.T) {
err := NewAPIError(500, "server error", "details", "req-123")
base := extractBaseError(err)
if base == nil {
t.Fatal("Expected non-nil BaseError")
}
if base.Code != 500 {
t.Errorf("Expected code 500, got %d", base.Code)
}
})
t.Run("unknown error type returns nil", func(t *testing.T) {
err := errors.New("plain error")
base := extractBaseError(err)
if base != nil {
t.Errorf("Expected nil for unknown error type, got %v", base)
}
})
}