-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
67 lines (56 loc) · 2.27 KB
/
Copy patherrors.go
File metadata and controls
67 lines (56 loc) · 2.27 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
package vabc
import (
"fmt"
"time"
)
// ErrKind classifies an API failure so the CLI can map it to a stable exit code
// without parsing messages. Importers can switch on it too.
type ErrKind string
const (
// KindNotFound: the target reports the resource does not exist (e.g. an invalid
// store number returns HTTP 400 "No Store exists ...").
KindNotFound ErrKind = "not_found"
// KindRateLimited: throttled or blocked (HTTP 429, a WAF challenge, or the local
// circuit-breaker). RetryAfter, when set, says how long to wait.
KindRateLimited ErrKind = "rate_limited"
// KindRetryable: a transient upstream/network error (5xx, timeout) worth retrying.
KindRetryable ErrKind = "retryable"
// KindSchemaDrift: the response did not match the expected shape — an upstream
// change, surfaced explicitly instead of as a decode panic.
KindSchemaDrift ErrKind = "schema_drift"
)
// APIError is a typed, classified error from the Virginia ABC client.
type APIError struct {
Kind ErrKind
Status int // HTTP status, when applicable (0 otherwise)
Msg string // human-readable summary
RetryAfter time.Duration // for KindRateLimited: suggested wait before retrying
Err error // wrapped cause, if any
}
func (e *APIError) Error() string {
if e.Status != 0 {
return fmt.Sprintf("%s (status %d): %s", e.Kind, e.Status, e.Msg)
}
return fmt.Sprintf("%s: %s", e.Kind, e.Msg)
}
func (e *APIError) Unwrap() error { return e.Err }
// RetryAfterSeconds returns the suggested retry delay in whole seconds (0 if none),
// for surfacing to an agent that wants to schedule a retry.
func (e *APIError) RetryAfterSeconds() int {
if e.RetryAfter <= 0 {
return 0
}
return int((e.RetryAfter + time.Second - 1) / time.Second)
}
func notFound(status int, msg string) *APIError {
return &APIError{Kind: KindNotFound, Status: status, Msg: msg}
}
func rateLimited(status int, msg string, retryAfter time.Duration) *APIError {
return &APIError{Kind: KindRateLimited, Status: status, Msg: msg, RetryAfter: retryAfter}
}
func retryable(status int, msg string, err error) *APIError {
return &APIError{Kind: KindRetryable, Status: status, Msg: msg, Err: err}
}
func schemaDrift(msg string, err error) *APIError {
return &APIError{Kind: KindSchemaDrift, Msg: msg, Err: err}
}