Repository navigation
Expand file tree
/
Copy patherrors.go
More file actions
99 lines (82 loc) · 2.49 KB
/
Copy patherrors.go
File metadata and controls
99 lines (82 loc) · 2.49 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
88
89
90
91
92
93
94
95
96
97
98
99
package typesafe
import (
"fmt"
"net/http"
"time"
)
type ErrorKind string
const (
KindHTTP ErrorKind = "http"
KindBadRequest ErrorKind = "bad_request"
KindAuthentication ErrorKind = "authentication"
KindPermissionDenied ErrorKind = "permission_denied"
KindNotFound ErrorKind = "not_found"
KindUnprocessableEntity ErrorKind = "unprocessable_entity"
KindRateLimit ErrorKind = "rate_limit"
KindServer ErrorKind = "server"
)
// APIError retains diagnostic data. Error intentionally excludes response bodies
// and messages, which can echo secrets. Inspect Message/Body only in a secure sink.
type APIError struct {
Kind ErrorKind
StatusCode int
Message string
Body []byte
Header http.Header
RequestID string
Endpoint string
RetryAfter *time.Duration
}
func (e *APIError) Error() string { return fmt.Sprintf("typesafe: HTTP %d (%s)", e.StatusCode, e.Kind) }
func kindForStatus(status int) ErrorKind {
switch status {
case 400:
return KindBadRequest
case 401:
return KindAuthentication
case 403:
return KindPermissionDenied
case 404:
return KindNotFound
case 422:
return KindUnprocessableEntity
case 429:
return KindRateLimit
default:
if status >= 500 {
return KindServer
}
return KindHTTP
}
}
type ValidationError struct{ Field, Message string }
func (e *ValidationError) Error() string {
return fmt.Sprintf("typesafe: invalid request %q: %s", e.Field, e.Message)
}
type ResponseValidationError struct {
Field string
Message string
HTTP *HTTPResponse
}
func (e *ResponseValidationError) Error() string {
return fmt.Sprintf("typesafe: invalid response at %q: %s", e.Field, e.Message)
}
type ConnectionError struct{ Cause error }
func (e *ConnectionError) Error() string { return "typesafe: connection or response-read failure" }
func (e *ConnectionError) Unwrap() error { return e.Cause }
type TimeoutError struct {
Cause error
Timeout time.Duration
}
func (e *TimeoutError) Error() string {
return fmt.Sprintf("typesafe: request timeout (%s)", e.Timeout)
}
func (e *TimeoutError) Unwrap() error { return e.Cause }
type ResponseTooLargeError struct{ Limit int64 }
func (e *ResponseTooLargeError) Error() string {
return fmt.Sprintf("typesafe: response exceeds %d bytes", e.Limit)
}
func invalid(field, msg string) error { return &ValidationError{Field: field, Message: msg} }
func invalidResponse(field, msg string) error {
return &ResponseValidationError{Field: field, Message: msg}
}