-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy patherrors.go
More file actions
49 lines (43 loc) · 1.39 KB
/
Copy patherrors.go
File metadata and controls
49 lines (43 loc) · 1.39 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
package listennotes
import (
"errors"
"fmt"
"net/http"
)
// Known errors can be matched with errors.Is.
var (
ErrBadRequest = errors.New("invalid API request")
ErrUnauthorized = errors.New("wrong API key, suspended account, or unavailable plan feature")
ErrForbidden = errors.New("the API account cannot perform this operation")
ErrNotFound = errors.New("endpoint or requested content does not exist")
ErrTooManyRequests = errors.New("API quota or rate limit exceeded")
ErrInternalServerError = errors.New("API server error")
ErrUnexpectedStatus = errors.New("unexpected HTTP status")
)
var errMap = map[int]error{
400: ErrBadRequest,
401: ErrUnauthorized,
403: ErrForbidden,
404: ErrNotFound,
429: ErrTooManyRequests,
}
// APIError retains the status, headers, and unmodified body of a non-2xx response.
// Use errors.As to access these fields and errors.Is to match a known error.
type APIError struct {
StatusCode int
Headers http.Header
Body string
}
func (e *APIError) Error() string {
return fmt.Sprintf("HTTP %d: %s", e.StatusCode, e.Body)
}
// Unwrap classifies errors without losing the server's response details.
func (e *APIError) Unwrap() error {
if mapped := errMap[e.StatusCode]; mapped != nil {
return mapped
}
if e.StatusCode >= 500 && e.StatusCode <= 599 {
return ErrInternalServerError
}
return ErrUnexpectedStatus
}