-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregistry.go
More file actions
203 lines (174 loc) · 4.84 KB
/
registry.go
File metadata and controls
203 lines (174 loc) · 4.84 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
package problem
import (
"net/http"
)
// BaseURI is an optional prefix that can be globally modified
// to set the base URI for all automatically generated Problem Types.
// However, since this is a library, hardcoding isn't ideal,
// so standard problem types typically use "about:blank" or a common schema.
// We'll use "about:blank" for standard HTTP errors to keep it generic,
// but users can define their own Types.
var BaseURI = "about:blank"
// TypeTemplate defines a reusable structure for a specific type of problem.
type TypeTemplate struct {
Type string
Title string
Status int
}
// New creates a new Problem instance based on a provided TypeTemplate,
// applying any functional Options.
func New(template TypeTemplate, opts ...Option) *Problem {
p := &Problem{
Type: template.Type,
Title: template.Title,
Status: template.Status,
}
for _, opt := range opts {
opt(p)
}
// Default fallback: if Type is empty but we're creating from a standard HTTP status template,
// we use a generic "about:blank" as recommended by RFC 7807 for simple HTTP codes.
if p.Type == "" {
p.Type = "about:blank"
}
return p
}
// Wrap is a convenience function that creates a new Problem from a template
// and automatically wraps the provided error.
// It is equivalent to New(template, WithErr(err)) with additional options.
func Wrap(err error, template TypeTemplate, opts ...Option) *Problem {
allOpts := make([]Option, 0, len(opts)+1)
allOpts = append(allOpts, WithErr(err))
allOpts = append(allOpts, opts...)
return New(template, allOpts...)
}
// ==== Standard HTTP Problem Templates ====
// These are pre-defined templates covering common HTTP 4xx and 5xx responses.
var (
// BadRequest (HTTP 400)
BadRequest = TypeTemplate{
Type: "about:blank",
Title: "Bad Request",
Status: http.StatusBadRequest,
}
// Unauthorized (HTTP 401)
Unauthorized = TypeTemplate{
Type: "about:blank",
Title: "Unauthorized",
Status: http.StatusUnauthorized,
}
// PaymentRequired (HTTP 402)
PaymentRequired = TypeTemplate{
Type: "about:blank",
Title: "Payment Required",
Status: http.StatusPaymentRequired,
}
// Forbidden (HTTP 403)
Forbidden = TypeTemplate{
Type: "about:blank",
Title: "Forbidden",
Status: http.StatusForbidden,
}
// NotFound (HTTP 404)
NotFound = TypeTemplate{
Type: "about:blank",
Title: "Not Found",
Status: http.StatusNotFound,
}
// MethodNotAllowed (HTTP 405)
MethodNotAllowed = TypeTemplate{
Type: "about:blank",
Title: "Method Not Allowed",
Status: http.StatusMethodNotAllowed,
}
// NotAcceptable (HTTP 406)
NotAcceptable = TypeTemplate{
Type: "about:blank",
Title: "Not Acceptable",
Status: http.StatusNotAcceptable,
}
// RequestTimeout (HTTP 408)
RequestTimeout = TypeTemplate{
Type: "about:blank",
Title: "Request Timeout",
Status: http.StatusRequestTimeout,
}
// Conflict (HTTP 409)
Conflict = TypeTemplate{
Type: "about:blank",
Title: "Conflict",
Status: http.StatusConflict,
}
// Gone (HTTP 410)
Gone = TypeTemplate{
Type: "about:blank",
Title: "Gone",
Status: http.StatusGone,
}
// LengthRequired (HTTP 411)
LengthRequired = TypeTemplate{
Type: "about:blank",
Title: "Length Required",
Status: http.StatusLengthRequired,
}
// PayloadTooLarge (HTTP 413)
PayloadTooLarge = TypeTemplate{
Type: "about:blank",
Title: "Payload Too Large",
Status: http.StatusRequestEntityTooLarge,
}
// URITooLong (HTTP 414)
URITooLong = TypeTemplate{
Type: "about:blank",
Title: "URI Too Long",
Status: http.StatusRequestURITooLong,
}
// UnsupportedMediaType (HTTP 415)
UnsupportedMediaType = TypeTemplate{
Type: "about:blank",
Title: "Unsupported Media Type",
Status: http.StatusUnsupportedMediaType,
}
// UnprocessableEntity (HTTP 422)
UnprocessableEntity = TypeTemplate{
Type: "about:blank",
Title: "Unprocessable Entity",
Status: http.StatusUnprocessableEntity,
}
// TooManyRequests (HTTP 429)
TooManyRequests = TypeTemplate{
Type: "about:blank",
Title: "Too Many Requests",
Status: http.StatusTooManyRequests,
}
// InternalServerError (HTTP 500)
InternalServerError = TypeTemplate{
Type: "about:blank",
Title: "Internal Server Error",
Status: http.StatusInternalServerError,
}
// NotImplemented (HTTP 501)
NotImplemented = TypeTemplate{
Type: "about:blank",
Title: "Not Implemented",
Status: http.StatusNotImplemented,
}
// BadGateway (HTTP 502)
BadGateway = TypeTemplate{
Type: "about:blank",
Title: "Bad Gateway",
Status: http.StatusBadGateway,
}
// ServiceUnavailable (HTTP 503)
ServiceUnavailable = TypeTemplate{
Type: "about:blank",
Title: "Service Unavailable",
Status: http.StatusServiceUnavailable,
}
// GatewayTimeout (HTTP 504)
GatewayTimeout = TypeTemplate{
Type: "about:blank",
Title: "Gateway Timeout",
Status: http.StatusGatewayTimeout,
}
)