-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
182 lines (148 loc) · 5.04 KB
/
errors.go
File metadata and controls
182 lines (148 loc) · 5.04 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
package hawksdk
import (
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"time"
)
// APIError is the base error type for all hawk API errors.
// It contains the HTTP status code, an error code string, a human-readable
// message, and optional details.
type APIError struct {
StatusCode int `json:"status_code"`
Code string `json:"code,omitempty"`
Message string `json:"message"`
Details string `json:"details,omitempty"`
}
// Error implements the error interface.
func (e *APIError) Error() string {
if e.Code != "" {
return fmt.Sprintf("hawk-sdk: %s [%s] (status %d)", e.Message, e.Code, e.StatusCode)
}
return fmt.Sprintf("hawk-sdk: %s (status %d)", e.Message, e.StatusCode)
}
// Unwrap returns nil for the base APIError.
func (e *APIError) Unwrap() error { return nil }
// BadRequestError represents a 400 Bad Request response.
type BadRequestError struct {
APIError
}
// Error implements the error interface.
func (e *BadRequestError) Error() string { return e.APIError.Error() }
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *BadRequestError) Unwrap() error { return &e.APIError }
// AuthenticationError represents a 401 Unauthorized response.
type AuthenticationError struct {
APIError
}
// Error implements the error interface.
func (e *AuthenticationError) Error() string { return e.APIError.Error() }
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *AuthenticationError) Unwrap() error { return &e.APIError }
// ForbiddenError represents a 403 Forbidden response.
type ForbiddenError struct {
APIError
}
// Error implements the error interface.
func (e *ForbiddenError) Error() string { return e.APIError.Error() }
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *ForbiddenError) Unwrap() error { return &e.APIError }
// NotFoundError represents a 404 Not Found response.
type NotFoundError struct {
APIError
}
// Error implements the error interface.
func (e *NotFoundError) Error() string { return e.APIError.Error() }
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *NotFoundError) Unwrap() error { return &e.APIError }
// RateLimitError represents a 429 Too Many Requests response.
// RetryAfter indicates how long to wait before retrying.
type RateLimitError struct {
APIError
RetryAfter time.Duration `json:"retry_after,omitempty"`
}
// Error implements the error interface.
func (e *RateLimitError) Error() string {
base := e.APIError.Error()
if e.RetryAfter > 0 {
return fmt.Sprintf("%s (retry after %s)", base, e.RetryAfter)
}
return base
}
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *RateLimitError) Unwrap() error { return &e.APIError }
// InternalServerError represents a 500 Internal Server Error response.
type InternalServerError struct {
APIError
}
// Error implements the error interface.
func (e *InternalServerError) Error() string { return e.APIError.Error() }
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *InternalServerError) Unwrap() error { return &e.APIError }
// ServiceUnavailableError represents a 503 Service Unavailable response.
type ServiceUnavailableError struct {
APIError
}
// Error implements the error interface.
func (e *ServiceUnavailableError) Error() string { return e.APIError.Error() }
// Unwrap allows errors.Is/As to match the underlying APIError.
func (e *ServiceUnavailableError) Unwrap() error { return &e.APIError }
// parseAPIError reads the response body and returns an appropriate typed error.
func parseAPIError(resp *http.Response) error {
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
var errResp ErrorResponse
msg := string(body)
code := ""
details := ""
if json.Unmarshal(body, &errResp) == nil && errResp.Error != "" {
msg = errResp.Error
code = errResp.Code
details = errResp.Details
}
base := APIError{
StatusCode: resp.StatusCode,
Code: code,
Message: msg,
Details: details,
}
switch resp.StatusCode {
case http.StatusBadRequest:
return &BadRequestError{APIError: base}
case http.StatusUnauthorized:
return &AuthenticationError{APIError: base}
case http.StatusForbidden:
return &ForbiddenError{APIError: base}
case http.StatusNotFound:
return &NotFoundError{APIError: base}
case http.StatusTooManyRequests:
retryAfter := parseRetryAfter(resp.Header.Get("Retry-After"))
return &RateLimitError{APIError: base, RetryAfter: retryAfter}
case http.StatusInternalServerError:
return &InternalServerError{APIError: base}
case http.StatusServiceUnavailable:
return &ServiceUnavailableError{APIError: base}
default:
return &base
}
}
// parseRetryAfter parses the Retry-After header value.
// It supports both seconds (integer) and HTTP-date formats.
func parseRetryAfter(val string) time.Duration {
if val == "" {
return 0
}
// Try parsing as seconds.
if secs, err := strconv.Atoi(val); err == nil {
return time.Duration(secs) * time.Second
}
// Try parsing as HTTP-date.
if t, err := http.ParseTime(val); err == nil {
d := time.Until(t)
if d > 0 {
return d
}
}
return 0
}