-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
218 lines (180 loc) · 4.91 KB
/
errors.go
File metadata and controls
218 lines (180 loc) · 4.91 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package runpod
import "fmt"
type APIError struct {
StatusCode int `json:"statusCode"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
Code string `json:"code,omitempty"`
RequestID string `json:"requestId,omitempty"`
}
func (e *APIError) Error() string {
if e.Code != "" {
return fmt.Sprintf("RunPod API Error %d (%s): %s - %s", e.StatusCode, e.Code, e.Message, e.Details)
}
return fmt.Sprintf("RunPod API Error %d (%s): %s", e.StatusCode, e.Code, e.Message)
}
func (e *APIError) IsNotFound() bool {
return e.StatusCode == 404
}
func (e *APIError) IsBadRequest() bool {
return e.StatusCode == 400
}
func (e *APIError) IsUnauthorized() bool {
return e.StatusCode == 401
}
func (e *APIError) IsForbidden() bool {
return e.StatusCode == 403
}
func (e *APIError) IsRateLimited() bool {
return e.StatusCode == 429
}
func (e *APIError) IsServerError() bool {
return e.StatusCode >= 500 && e.StatusCode < 600
}
func (e *APIError) IsClientError() bool {
return e.StatusCode >= 400 && e.StatusCode < 500
}
type ValidationError struct {
Field string `json:"field"`
Message string `json:"message"`
Value interface{} `json:"value,omitempty"`
}
func (e *ValidationError) Error() string {
if e.Value != nil {
return fmt.Sprintf("validation error for field '%s': %s (value: %v)", e.Field, e.Message, e.Value)
}
return fmt.Sprintf("validation error for field '%s': %s", e.Field, e.Message)
}
type ValidationErrors []ValidationError
func (ve ValidationErrors) Error() string {
if len(ve) == 1 {
return ve[0].Error()
}
return fmt.Sprintf("multiple validation errors: %d errors", len(ve))
}
type NetworkError struct {
Message string
Cause error
}
// Error implements the error interface
func (e *NetworkError) Error() string {
if e.Cause != nil {
return fmt.Sprintf("network error: %s (caused by: %v)", e.Message, e.Cause)
}
return fmt.Sprintf("network error: %s", e.Message)
}
// Unwrap implements the unwrapper interface for error chains
func (e *NetworkError) Unwrap() error {
return e.Cause
}
type TimeoutError struct {
Operation string
Duration string
}
func (e *TimeoutError) Error() string {
return fmt.Sprintf("timeout error: %s operation timed out after %s", e.Operation, e.Duration)
}
// AuthError represents an authentication error
type AuthError struct {
Message string
}
// Error implements the error interface
func (e *AuthError) Error() string {
return fmt.Sprintf("authentication error: %s", e.Message)
}
// RateLimitError represents a rate limiting error
type RateLimitError struct {
Message string
RetryAfter string
Limit int
Remaining int
ResetTime string
}
// Error implements the error interface
func (e *RateLimitError) Error() string {
return fmt.Sprintf("rate limit exceeded: %s (retry after: %s)", e.Message, e.RetryAfter)
}
// NewAPIError creates a new API error
func NewAPIError(statusCode int, message string) *APIError {
return &APIError{
StatusCode: statusCode,
Message: message,
}
}
func NewAPIErrorWithDetails(statusCode int, message, details string) *APIError {
return &APIError{
StatusCode: statusCode,
Message: message,
Details: details,
}
}
func NewValidationError(field, message string) *ValidationError {
return &ValidationError{
Field: field,
Message: message,
}
}
func NewValidationErrorWithValue(field, message string, value interface{}) *ValidationError {
return &ValidationError{
Field: field,
Message: message,
Value: value,
}
}
func NewNetworkError(message string, cause error) *NetworkError {
return &NetworkError{
Message: message,
Cause: cause,
}
}
func NewTimeoutError(operation, duration string) *TimeoutError {
return &TimeoutError{
Operation: operation,
Duration: duration,
}
}
// NewAuthError creates a new authentication error
func NewAuthError(message string) *AuthError {
return &AuthError{
Message: message,
}
}
func NewRateLimitError(message, retryAfter string) *RateLimitError {
return &RateLimitError{
Message: message,
RetryAfter: retryAfter,
}
}
// ================================
// ERROR CHECKING HELPERS
// ================================
// IsAPIError checks if an error is an APIError
func IsAPIError(err error) bool {
_, ok := err.(*APIError)
return ok
}
// IsValidationError checks if an error is a ValidationError
func IsValidationError(err error) bool {
_, ok := err.(*ValidationError)
return ok
}
// IsNetworkError checks if an error is a NetworkError
func IsNetworkError(err error) bool {
_, ok := err.(*NetworkError)
return ok
}
// IsTimeoutError checks if an error is a TimeoutError
func IsTimeoutError(err error) bool {
_, ok := err.(*TimeoutError)
return ok
}
// IsAuthError checks if an error is an AuthError
func IsAuthError(err error) bool {
_, ok := err.(*AuthError)
return ok
}
// IsRateLimitError checks if an error is a RateLimitError
func IsRateLimitError(err error) bool {
_, ok := err.(*RateLimitError)
return ok
}