-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
134 lines (111 loc) · 3.78 KB
/
Copy patherrors.go
File metadata and controls
134 lines (111 loc) · 3.78 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package openranking
import (
"fmt"
"net/http"
"strconv"
"time"
)
// Error represent an HTTP error with the specified code and reason.
// If the reason is not empty, it is written in the "X-Reason" header as per ORE-00.
type Error struct {
Code int
Reason string
}
func (e Error) Error() string {
return fmt.Sprintf("code: %d, reason: %s", e.Code, e.Reason)
}
func (e Error) String() string {
return e.Error()
}
func (e *Error) Is(target error) bool {
if e == nil {
return target == nil
}
err, ok := target.(Error)
if !ok {
return false
}
return e.Code == err.Code && e.Reason == err.Reason
}
// WriteError writes the error to the http response. If the reason is non-empty,
// it writes it to the "X-Reason" header as per ORE-00.
func WriteError(w http.ResponseWriter, e *Error) {
if e == nil {
return
}
if e.Reason != "" {
w.Header().Set("X-Reason", e.Reason)
}
http.Error(w, "", e.Code)
}
// Retry is returned when the provider responds with 202 Accepted.
// The result is not yet ready. Retry the identical request after After.
type Retry struct {
After time.Duration
}
func (r Retry) Error() string {
return fmt.Sprintf("result not ready, retry after %s", r.After)
}
// WriteRetry writes a 202 Accepted response with a Retry-After header.
func WriteRetry(w http.ResponseWriter, after time.Duration) {
w.Header().Set("Retry-After", strconv.Itoa(int(after.Seconds())))
w.WriteHeader(http.StatusAccepted)
}
func (r *Retry) Is(target error) bool {
if r == nil {
return target == nil
}
t, ok := target.(*Retry)
if !ok {
return false
}
return r.After == t.After
}
// ErrBadRequest returns a 400 Bad Request error with the given reason.
func ErrBadRequest(reason string) *Error {
return &Error{Code: http.StatusBadRequest, Reason: reason}
}
// ErrUnauthorized returns a 401 Unauthorized error with the given reason.
func ErrUnauthorized(reason string) *Error {
return &Error{Code: http.StatusUnauthorized, Reason: reason}
}
// ErrPaymentRequired returns a 402 Payment Required error with the given reason.
func ErrPaymentRequired(reason string) *Error {
return &Error{Code: http.StatusPaymentRequired, Reason: reason}
}
// ErrForbidden returns a 403 Forbidden error with the given reason.
func ErrForbidden(reason string) *Error {
return &Error{Code: http.StatusForbidden, Reason: reason}
}
// ErrNotFound returns a 404 Not Found error with the given reason.
func ErrNotFound(reason string) *Error {
return &Error{Code: http.StatusNotFound, Reason: reason}
}
// ErrNotAllowed returns a 405 Method Not Allowed error with the given reason.
func ErrNotAllowed(reason string) *Error {
return &Error{Code: http.StatusMethodNotAllowed, Reason: reason}
}
// ErrTooLarge returns a 413 Payload Too Large error with the given reason.
func ErrTooLarge(reason string) *Error {
return &Error{Code: http.StatusRequestEntityTooLarge, Reason: reason}
}
// ErrUnsupportedMedia returns a 415 Unsupported Media Type error with the given reason.
func ErrUnsupportedMedia(reason string) *Error {
return &Error{Code: http.StatusUnsupportedMediaType, Reason: reason}
}
// ErrTooMany returns a 429 Too Many Requests error with the given reason.
func ErrTooMany(reason string) *Error {
return &Error{Code: http.StatusTooManyRequests, Reason: reason}
}
// ErrInternal returns a 500 Internal Server Error with the given reason.
func ErrInternal(reason string) *Error {
return &Error{Code: http.StatusInternalServerError, Reason: reason}
}
// ErrNotImplemented returns a 501 Not Implemented error with the given reason.
func ErrNotImplemented(reason string) *Error {
return &Error{Code: http.StatusNotImplemented, Reason: reason}
}
// ErrUnavailable returns a 503 Service Unavailable error with the given reason.
func ErrUnavailable(reason string) *Error {
return &Error{Code: http.StatusServiceUnavailable, Reason: reason}
}