-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem.go
297 lines (230 loc) · 7.33 KB
/
problem.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package akumu
import (
_ "embed"
"encoding/json"
"fmt"
"maps"
"net/http"
"github.com/studiolambda/akumu/utils"
)
// Problem represents a problem details for HTTP APIs.
// See https://datatracker.ietf.org/doc/html/rfc9457 for more information.
type Problem struct {
// err stores the native error of the problem if
// it happens to have one. This struct member can be
// nil if the problem was created manually.
//
// Refer to [NewProblem] to automatically assign an error
// to the new Problem.
err error
// additional stores additional metadata that will be
// appended to the problem JSON representation. It is used
// to serialize and de-serialize properties.
additional map[string]any
Type string
Title string
Detail string
Status int
Instance string
}
type ProblemControlsResolver[R any] func(problem Problem, request *http.Request) R
type ProblemControls struct {
Lowercase ProblemControlsResolver[bool]
DefaultStatus ProblemControlsResolver[int]
DefaultType ProblemControlsResolver[string]
DefaultTitle ProblemControlsResolver[string]
DefaultInstance ProblemControlsResolver[string]
Response ProblemControlsResolver[Builder]
}
// ProblemsKey is the context key where the
// problem controls are stored in the request.
type ProblemsKey struct{}
func defaultedProblemControls(controls ProblemControls) ProblemControls {
if controls.Lowercase == nil {
controls.Lowercase = defaultProblemControlsLowercase
}
if controls.DefaultStatus == nil {
controls.DefaultStatus = defaultProblemControlsStatus
}
if controls.DefaultType == nil {
controls.DefaultType = defaultProblemControlsType
}
if controls.DefaultTitle == nil {
controls.DefaultTitle = defaultProblemControlsTitle
}
if controls.DefaultInstance == nil {
controls.DefaultInstance = defaultProblemControlsInstance
}
if controls.Response == nil {
controls.Response = defaultProblemControlsResponse
}
return controls
}
// Problems return the [ProblemControls] used to determine
// how [Problem] respond to http requests.
func Problems(request *http.Request) (ProblemControls, bool) {
controls, ok := request.
Context().
Value(ProblemsKey{}).(ProblemControls)
return controls, ok
}
func defaultProblemControlsLowercase(problem Problem, request *http.Request) bool {
return true
}
func defaultProblemControlsStatus(problem Problem, request *http.Request) int {
return http.StatusInternalServerError
}
func defaultProblemControlsType(problem Problem, request *http.Request) string {
return "about:blank"
}
func defaultProblemControlsTitle(problem Problem, request *http.Request) string {
return http.StatusText(problem.Status)
}
func defaultProblemControlsInstance(problem Problem, request *http.Request) string {
return request.URL.String()
}
func ProblemControlsResponseFrom(responses map[string]Builder) ProblemControlsResolver[Builder] {
return func(problem Problem, request *http.Request) Builder {
accept := utils.ParseAccept(request)
for _, media := range accept.Order() {
if response, ok := responses[media]; ok {
return response
}
}
return Response(problem.Status).
Text(fmt.Sprintf("%d %s\n\n%s", problem.Status, problem.Title, problem.Detail))
}
}
func defaultProblemControlsResponse(problem Problem, request *http.Request) Builder {
responses := map[string]Builder{
"application/problem+json": Response(problem.Status).
JSON(problem).
Header("Content-Type", "application/problem+json"),
"application/json": Response(problem.Status).
JSON(problem).
Header("Content-Type", "application/problem+json"),
}
return ProblemControlsResponseFrom(responses)(problem, request)
}
// NewProblem creates a new [Problem] from
// the given error and status code.
func NewProblem(err error, status int) Problem {
return Problem{
err: err,
additional: make(map[string]any),
Detail: err.Error(),
Status: status,
}
}
// Additional returns the additional value of the given key.
//
// Use [Problem.With] to add additional values.
// Use [Problem.Without] to remove additional values.
func (problem Problem) Additional(key string) (any, bool) {
additional, ok := problem.additional[key]
return additional, ok
}
// With adds a new additional value to the given key.
func (problem Problem) With(key string, value any) Problem {
if problem.additional == nil {
problem.additional = map[string]any{key: value}
return problem
}
problem.additional = maps.Clone(problem.additional)
problem.additional[key] = value
return problem
}
// Without removes an additional value to the given key.
func (problem Problem) Without(key string) Problem {
if problem.additional == nil {
return problem
}
problem.additional = maps.Clone(problem.additional)
delete(problem.additional, key)
return problem
}
// Error is the error-like string representation of a [Problem].
func (problem Problem) Error() string {
if problem.err != nil {
return fmt.Sprintf("%d %s: %s", problem.Status, http.StatusText(problem.Status), problem.err)
}
return problem.Title
}
func (problem Problem) Unwrap() error {
return problem.err
}
// MarshalJSON replaces the default JSON encoding behaviour.
func (problem Problem) MarshalJSON() ([]byte, error) {
mapped := make(map[string]any, len(problem.additional)+5)
mapped["detail"] = problem.Detail
mapped["instance"] = problem.Instance
mapped["status"] = problem.Status
mapped["title"] = problem.Title
mapped["type"] = problem.Type
for key, value := range problem.additional {
mapped[key] = value
}
return json.Marshal(mapped)
}
// UnmarshalJSON replaces the default JSON decoding behaviour.
func (problem *Problem) UnmarshalJSON(data []byte) error {
mapped := make(map[string]any)
if err := json.Unmarshal(data, &mapped); err != nil {
return err
}
if value, ok := mapped["detail"].(string); ok {
problem.Detail = value
}
if value, ok := mapped["instance"].(string); ok {
problem.Instance = value
}
if value, ok := mapped["status"].(float64); ok {
problem.Status = int(value)
}
if value, ok := mapped["title"].(string); ok {
problem.Title = value
}
if value, ok := mapped["type"].(string); ok {
problem.Type = value
}
delete(mapped, "detail")
delete(mapped, "instance")
delete(mapped, "status")
delete(mapped, "title")
delete(mapped, "type")
problem.additional = mapped
return nil
}
func (problem Problem) controls(request *http.Request) ProblemControls {
controls := ProblemControls{}
if c, ok := Problems(request); ok {
controls = c
}
return defaultedProblemControls(controls)
}
func (problem Problem) defaulted(request *http.Request) Problem {
controls := problem.controls(request)
if problem.Type == "" {
problem.Type = controls.DefaultType(problem, request)
}
if problem.Status == 0 {
problem.Status = controls.DefaultStatus(problem, request)
}
if problem.Title == "" {
problem.Title = controls.DefaultTitle(problem, request)
}
if problem.Instance == "" {
problem.Instance = controls.DefaultInstance(problem, request)
}
if controls.Lowercase(problem, request) {
problem.Title = lowercase(problem.Title)
problem.Detail = lowercase(problem.Detail)
}
return problem
}
// Respond implements [Responder] interface to implement
// how a problem responds to an http request.
func (problem Problem) Respond(request *http.Request) Builder {
controls := problem.controls(request)
return controls.Response(problem.defaulted(request), request)
}