-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
70 lines (55 loc) · 1.66 KB
/
Copy patherrors.go
File metadata and controls
70 lines (55 loc) · 1.66 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
package decodo
// DecodoError is the base error type for all Decodo API errors.
type DecodoError struct {
StatusCode int
APIStatus string
Message string
}
func (e *DecodoError) Error() string {
return e.Message
}
// AuthenticationError is returned on 401/403 responses.
type AuthenticationError struct {
DecodoError
}
func (e *AuthenticationError) Unwrap() error { return &e.DecodoError }
// RateLimitError is returned on 429 responses.
type RateLimitError struct {
DecodoError
}
func (e *RateLimitError) Unwrap() error { return &e.DecodoError }
// ValidationError is returned on 422/400 responses with validation errors.
type ValidationError struct {
DecodoError
Errors []interface{}
}
func (e *ValidationError) Unwrap() error { return &e.DecodoError }
type ConfigurationError struct {
Msg string
}
func (e *ConfigurationError) Error() string { return e.Msg }
// TimeoutError is returned when a request times out.
type TimeoutError struct {
Msg string
}
func (e *TimeoutError) Error() string {
return e.Msg
}
// CancellationError is returned when the caller's context is cancelled.
type CancellationError struct {
Msg string
}
func (e *CancellationError) Error() string { return e.Msg }
func mapHTTPError(statusCode int, message, apiStatus string, errors []interface{}) error {
base := DecodoError{StatusCode: statusCode, APIStatus: apiStatus, Message: message}
switch {
case statusCode == 401 || statusCode == 403:
return &AuthenticationError{base}
case statusCode == 429:
return &RateLimitError{base}
case statusCode == 422 || (statusCode == 400 && len(errors) > 0):
return &ValidationError{DecodoError: base, Errors: errors}
default:
return &base
}
}