-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
68 lines (60 loc) · 1.78 KB
/
Copy patherrors.go
File metadata and controls
68 lines (60 loc) · 1.78 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
package aion2
import (
"errors"
"fmt"
"net/http"
"strings"
"time"
)
// maxErrorBodyBytes is the cap size for the err body to be logged
const maxErrorBodyBytes = 512
var (
ErrNotFound = errors.New("aion2: not found")
ErrUpstream = errors.New("aion2: upstream error")
ErrBadRequest = errors.New("aion2: upstream rejected params")
ErrRateLimited = errors.New("aion2: rate limited")
ErrNoSeason = errors.New("aion2: no ranking season")
ErrUnsupportedRegion = errors.New("aion2: unsupported region")
ErrFeatureUnavailable = errors.New("aion2: feature unavailable on this region")
)
type APIError struct {
Region Region
Feature Feature
Path string // the URL without its query, which carries character IDs
Body string // upstream's bytes, truncated; never an SDK message
StatusCode int // upstream's status, when the status is not success
RetryAfter time.Duration // upstream's Retry-After, if it sent one
Err error
}
func (e *APIError) Error() string {
var b strings.Builder
fmt.Fprintf(&b, "%v (%s %s", e.Err, e.Region, e.Feature)
if e.Path != "" {
fmt.Fprintf(&b, " %s", e.Path)
}
if e.StatusCode != 0 {
fmt.Fprintf(&b, " http %d", e.StatusCode)
}
b.WriteByte(')')
if e.Body != "" {
fmt.Fprintf(&b, ": %s", e.Body)
}
return b.String()
}
func (e *APIError) Unwrap() error { return e.Err }
func sentinelFor(status int) error {
switch {
case status >= 200 && status < 300:
return nil
case status == http.StatusBadRequest:
return ErrBadRequest
case status == http.StatusNotFound:
return ErrNotFound
case status == http.StatusTooManyRequests:
return ErrRateLimited
}
return ErrUpstream
}
func clip(body []byte) string {
return string(body[:min(len(body), maxErrorBodyBytes)])
}