Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
61 lines (53 loc) · 1.99 KB
/
Copy patherrors.go
File metadata and controls
61 lines (53 loc) · 1.99 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
package mowen
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
// ErrResponseTooLarge indicates that a response exceeded the 8 MiB limit.
var ErrResponseTooLarge = errors.New("mowen: response exceeds 8 MiB")
// requestError keeps transport diagnostics available through explicit unwrapping
// without exposing URLs or nested redirect headers in ordinary error logs.
type requestError struct{ cause error }
func (e *requestError) Error() string { return "mowen: request failed" }
func (e *requestError) Unwrap() error { return e.cause }
// Error reasons documented by Mowen. Prefer these over numeric API codes.
const (
ReasonLogin = "LOGIN"
ReasonParams = "PARAMS"
ReasonPermission = "PERM"
ReasonNotFound = "NOT_FOUND"
ReasonRateLimit = "RATELIMIT"
ReasonRisky = "RISKY"
ReasonBlocked = "BLOCKED"
ReasonQuota = "QUOTA"
)
// APIError preserves an unsuccessful HTTP response, including non-JSON bodies.
// Message, Body, Details and metadata may contain private data. Error deliberately
// omits response text; inspect these fields explicitly when diagnosing a request.
type APIError struct {
StatusCode int `json:"-"`
Code int `json:"code"`
Reason string `json:"reason"`
Message string `json:"message"`
Metadata map[string]string `json:"metadata"`
Meta map[string]string `json:"meta"`
Details []json.RawMessage `json:"details"`
RequestID string `json:"-"`
RetryAfter string `json:"-"`
Body []byte `json:"-"`
}
func (e *APIError) Error() string {
return fmt.Sprintf("mowen: HTTP %d (API code %d)", e.StatusCode, e.Code)
}
func newAPIError(resp *http.Response, body []byte) *APIError {
e := &APIError{}
// Non-JSON and differently shaped errors still retain status and raw body.
_ = json.Unmarshal(body, e)
e.StatusCode = resp.StatusCode
e.RequestID = resp.Header.Get("X-Request-Id")
e.RetryAfter = resp.Header.Get("Retry-After")
e.Body = body
return e
}