-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
214 lines (175 loc) · 5.52 KB
/
Copy patherrors.go
File metadata and controls
214 lines (175 loc) · 5.52 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package xhttp
import (
"context"
"encoding/json"
"fmt"
"net/http"
)
type ProblemDetail struct {
Status int `json:"status"`
Type string `json:"type"`
Title string `json:"title"`
}
type InvalidParam struct {
Name string `json:"name"`
Reason string `json:"reason"`
}
type ValidationProblemDetail struct {
ProblemDetail
InvalidParams []InvalidParam `json:"invalid-params"`
}
var _ error = (*BadRequestError)(nil)
var _ HttpProblemWriter = (*BadRequestError)(nil)
func NewBadRequestError(invalidParams ...InvalidParam) *BadRequestError {
return &BadRequestError{invalidParams: invalidParams}
}
type BadRequestError struct {
invalidParams []InvalidParam
}
func (b *BadRequestError) Error() string {
return fmt.Sprintf("%v", b.invalidParams)
}
func (b *BadRequestError) WriteProblem(_ context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusBadRequest)
w.Header().Set("Content-Type", "application/problem+json")
detail := ValidationProblemDetail{
ProblemDetail: ProblemDetail{
Status: http.StatusBadRequest,
Type: "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1",
Title: "Request parameters did not validate",
},
InvalidParams: b.invalidParams,
}
return json.NewEncoder(w).Encode(detail)
}
var _ error = (*UnauthorizedError)(nil)
var _ HttpProblemWriter = (*UnauthorizedError)(nil)
type UnauthorizedError struct {
message string
}
func NewUnauthorizedError(message string) *UnauthorizedError {
return &UnauthorizedError{message: message}
}
func (f *UnauthorizedError) WriteProblem(ctx context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusUnauthorized)
w.Header().Set("Content-Type", "application/problem+json")
detail := ProblemDetail{
Status: http.StatusUnauthorized,
Type: "https://datatracker.ietf.org/doc/html/rfc7235#section-3.1",
Title: f.message,
}
return json.NewEncoder(w).Encode(detail)
}
func (f *UnauthorizedError) Error() string {
return f.message
}
var _ error = (*ForbiddenError)(nil)
var _ HttpProblemWriter = (*ForbiddenError)(nil)
type ForbiddenError struct {
message string
}
func NewForbiddenError(message string) *ForbiddenError {
return &ForbiddenError{message: message}
}
func (f *ForbiddenError) WriteProblem(ctx context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusForbidden)
w.Header().Set("Content-Type", "application/problem+json")
detail := ProblemDetail{
Status: http.StatusForbidden,
Type: "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.3",
Title: f.message,
}
return json.NewEncoder(w).Encode(detail)
}
func (f *ForbiddenError) Error() string {
return f.message
}
var _ error = &NotFoundError{}
var _ HttpProblemWriter = &NotFoundError{}
func NewNotFoundError(message string) *NotFoundError {
return &NotFoundError{
message: message,
}
}
type NotFoundError struct {
message string
}
func (n *NotFoundError) Error() string {
return n.message
}
func (n *NotFoundError) WriteProblem(ctx context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusNotFound)
w.Header().Set("Content-Type", "application/problem+json")
detail := ProblemDetail{
Status: http.StatusNotFound,
Type: "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.4",
Title: n.message,
}
return json.NewEncoder(w).Encode(detail)
}
var _ error = (*UnsupportedMediaType)(nil)
var _ HttpProblemWriter = (*UnsupportedMediaType)(nil)
type UnsupportedMediaType struct {
}
func NewUnsupportedMediaType() *UnsupportedMediaType {
return &UnsupportedMediaType{}
}
func (u *UnsupportedMediaType) WriteProblem(ctx context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusUnsupportedMediaType)
w.Header().Set("Content-Type", "application/problem+json")
detail := ProblemDetail{
Status: http.StatusForbidden,
Type: "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.13",
Title: "unsupported media type",
}
return json.NewEncoder(w).Encode(detail)
}
func (u *UnsupportedMediaType) Error() string {
return "unsupported media type"
}
var _ error = (*InternalServerError)(nil)
var _ HttpProblemWriter = (*InternalServerError)(nil)
func NewInternalServerError(err error) *InternalServerError {
return &InternalServerError{
innerError: err,
}
}
type InternalServerError struct {
innerError error
}
func (i *InternalServerError) Error() string {
return i.innerError.Error()
}
func (i *InternalServerError) WriteProblem(_ context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusInternalServerError)
w.Header().Set("Content-Type", "application/problem+json")
detail := ProblemDetail{
Status: http.StatusInternalServerError,
Type: "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1",
Title: "Internal Server Error",
}
return json.NewEncoder(w).Encode(detail)
}
var _ error = (*ServiceUnavailableError)(nil)
var _ HttpProblemWriter = (*ServiceUnavailableError)(nil)
func NewServiceUnavailableError(err error) *ServiceUnavailableError {
return &ServiceUnavailableError{
innerError: err,
}
}
type ServiceUnavailableError struct {
innerError error
}
func (s *ServiceUnavailableError) WriteProblem(ctx context.Context, w http.ResponseWriter) error {
w.WriteHeader(http.StatusServiceUnavailable)
w.Header().Set("Content-Type", "application/problem+json")
detail := ProblemDetail{
Status: http.StatusServiceUnavailable,
Type: "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.4",
Title: "The server is unavailable",
}
return json.NewEncoder(w).Encode(detail)
}
func (s *ServiceUnavailableError) Error() string {
return s.innerError.Error()
}