forked from Kong/go-kong
/
error.go
84 lines (72 loc) · 1.84 KB
/
error.go
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
package kong
import (
"errors"
"fmt"
"net/http"
"time"
)
// APIError is used for Kong Admin API errors.
type APIError struct {
httpCode int
message string
raw []byte
details any
}
func NewAPIError(code int, msg string) *APIError {
return &APIError{
httpCode: code,
message: msg,
}
}
func NewAPIErrorWithRaw(code int, msg string, raw []byte) *APIError {
return &APIError{
httpCode: code,
message: msg,
raw: raw,
}
}
func (e *APIError) Error() string {
return fmt.Sprintf("HTTP status %d (message: %q)", e.httpCode, e.message)
}
// Code returns the HTTP status code for the error.
func (e *APIError) Code() int {
return e.httpCode
}
// Raw returns the raw HTTP error response body.
func (e *APIError) Raw() []byte {
return e.raw
}
// Details returns optional details that might be relevant for proper
// handling of the APIError on the caller side.
func (e *APIError) Details() any {
return e.details
}
// SetDetails allows setting optional details that might be relevant
// for proper handling of the APIError on the caller side.
func (e *APIError) SetDetails(details any) {
e.details = details
}
// IsNotFoundErr returns true if the error or it's cause is
// a 404 response from Kong.
func IsNotFoundErr(e error) bool {
var apiErr *APIError
if errors.As(e, &apiErr) {
return apiErr.httpCode == http.StatusNotFound
}
return false
}
// IsForbiddenErr returns true if the error or its cause is
// a 403 response from Kong.
func IsForbiddenErr(e error) bool {
var apiErr *APIError
if errors.As(e, &apiErr) {
return apiErr.httpCode == http.StatusForbidden
}
return false
}
// ErrTooManyRequestsDetails is expected to be available under APIError.Details()
// when the API returns status code 429 (Too many requests) and a `Retry-After` header
// is set.
type ErrTooManyRequestsDetails struct {
RetryAfter time.Duration
}