-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
83 lines (72 loc) · 2.11 KB
/
Copy patherrors.go
File metadata and controls
83 lines (72 loc) · 2.11 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
package elleven
import (
"encoding/json"
"fmt"
)
// APIError represents an error returned by the ERP Elleven API.
// It carries the HTTP status code, parsed API messages, and the raw response body.
type APIError struct {
// StatusCode is the HTTP status code returned by the server.
StatusCode int
// Messages are the API-level messages parsed from the response body.
Messages []APIMessage
// RawBody is the raw response body for debugging.
RawBody []byte
}
// Error implements the error interface.
func (e *APIError) Error() string {
if len(e.Messages) > 0 {
return fmt.Sprintf("elleven: API error %d: %s", e.StatusCode, e.Messages[0].Message)
}
return fmt.Sprintf("elleven: API error %d: %s", e.StatusCode, string(e.RawBody))
}
// IsNotFound returns true if the error is an HTTP 404 Not Found.
func IsNotFound(err error) bool {
if e, ok := err.(*APIError); ok {
return e.StatusCode == 404
}
return false
}
// IsUnauthorized returns true if the error is an HTTP 401 Unauthorized.
func IsUnauthorized(err error) bool {
if e, ok := err.(*APIError); ok {
return e.StatusCode == 401
}
return false
}
// IsForbidden returns true if the error is an HTTP 403 Forbidden.
func IsForbidden(err error) bool {
if e, ok := err.(*APIError); ok {
return e.StatusCode == 403
}
return false
}
// IsRateLimited returns true if the error is an HTTP 429 Too Many Requests.
func IsRateLimited(err error) bool {
if e, ok := err.(*APIError); ok {
return e.StatusCode == 429
}
return false
}
// IsServerError returns true if the error is an HTTP 5xx server error.
func IsServerError(err error) bool {
if e, ok := err.(*APIError); ok {
return e.StatusCode >= 500
}
return false
}
// parseAPIError attempts to parse an API error from a raw response body.
func parseAPIError(statusCode int, body []byte) *APIError {
apiErr := &APIError{
StatusCode: statusCode,
RawBody: body,
}
var envelope struct {
Success bool `json:"success"`
Messages []APIMessage `json:"messages"`
}
if err := json.Unmarshal(body, &envelope); err == nil && len(envelope.Messages) > 0 {
apiErr.Messages = envelope.Messages
}
return apiErr
}