Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
87 lines (76 loc) · 2.61 KB
/
Copy patherrors.go
File metadata and controls
87 lines (76 loc) · 2.61 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
package edeka
import (
"encoding/xml"
"errors"
"fmt"
"net/http"
"strconv"
"time"
)
// SOAPFaultError represents a SOAP fault returned by the API with HTTP 500.
type SOAPFaultError struct {
Code string
String string
}
// ErrUnsupportedAppVersion reports that the server rejected applicationVersion.
var ErrUnsupportedAppVersion = errors.New("app version no longer supported by the server")
// faultUnsupportedAppVersion comes back from every SOAP operation, not just registration.
const faultUnsupportedAppVersion = "10920"
func (e *SOAPFaultError) Error() string {
if e.String == faultUnsupportedAppVersion {
return fmt.Sprintf("SOAP fault: [%s] %s (%s)", e.Code, e.String, ErrUnsupportedAppVersion)
}
return fmt.Sprintf("SOAP fault: [%s] %s", e.Code, e.String)
}
// Is maps faultstring 10920 onto ErrUnsupportedAppVersion.
func (e *SOAPFaultError) Is(target error) bool {
return target == ErrUnsupportedAppVersion && e.String == faultUnsupportedAppVersion
}
// RateLimitError is returned when the API responds with HTTP 429.
type RateLimitError struct {
RetryAfter time.Duration
RawRetryAfter string // original Retry-After header value, empty if absent
}
// Cap Retry-After at 24h. A server asking us to wait longer isn't useful,
// and unbounded values can overflow time.Duration.
const maxRetryAfterSeconds = 24 * 60 * 60
func newRateLimitError(header string) *RateLimitError {
e := &RateLimitError{RawRetryAfter: header}
if secs, err := strconv.Atoi(header); err == nil && secs > 0 {
if secs > maxRetryAfterSeconds {
secs = maxRetryAfterSeconds
}
e.RetryAfter = time.Duration(secs) * time.Second
} else if t, err := http.ParseTime(header); err == nil {
if d := time.Until(t); d > 0 {
if d > maxRetryAfterSeconds*time.Second {
d = maxRetryAfterSeconds * time.Second
}
e.RetryAfter = d
}
}
return e
}
func (e *RateLimitError) Error() string {
if e.RetryAfter > 0 {
return fmt.Sprintf("rate limited - retry after %s", e.RetryAfter)
}
if e.RawRetryAfter != "" {
return fmt.Sprintf("rate limited (unparsed Retry-After: %q)", e.RawRetryAfter)
}
return "rate limited"
}
// soapFaultEnvelope is the unmarshal target used to probe SOAP responses for
// a Fault element without knowing the full response shape.
type soapFaultEnvelope struct {
XMLName xml.Name `xml:"Envelope"`
Body struct {
// Pointer so we can distinguish "no Fault element" (nil) from
// "Fault element present but empty". A present-but-empty Fault
// is still a server-side anomaly worth surfacing.
Fault *struct {
Code string `xml:"faultcode"`
String string `xml:"faultstring"`
} `xml:"Fault"`
} `xml:"Body"`
}