-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.go
More file actions
222 lines (186 loc) · 3.9 KB
/
problem.go
File metadata and controls
222 lines (186 loc) · 3.9 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
215
216
217
218
219
220
221
222
package problem
import (
"encoding/json"
"errors"
"maps"
"net/http"
)
const MediaType = "application/problem+json"
type Option interface{ apply(*Problem) }
type optionFunc func(*Problem)
func (f optionFunc) apply(p *Problem) { f(p) }
type Problem struct {
data map[string]any
cause error
}
func New(opts ...Option) *Problem {
p := &Problem{data: make(map[string]any)}
for _, opt := range opts {
if opt != nil {
opt.apply(p)
}
}
return p
}
func Of(status int) *Problem {
return New(Status(status), Title(http.StatusText(status)))
}
func (p *Problem) Append(opts ...Option) *Problem {
if p == nil {
return New(opts...)
}
if p.data == nil {
p.data = make(map[string]any)
}
for _, opt := range opts {
if opt != nil {
opt.apply(p)
}
}
return p
}
func (p *Problem) Data() map[string]any {
if p == nil || p.data == nil {
return map[string]any{}
}
out := make(map[string]any, len(p.data))
maps.Copy(out, p.data)
return out
}
func (p *Problem) Get(key string) (any, bool) {
if p == nil || p.data == nil {
return nil, false
}
v, ok := p.data[key]
return v, ok
}
func (p *Problem) JSON() []byte {
b, _ := json.Marshal(p)
return b
}
func (p *Problem) JSONString() string { return string(p.JSON()) }
func (p *Problem) Error() string { return p.JSONString() }
func (p *Problem) Unwrap() error {
if p == nil {
return nil
}
return p.cause
}
func (p *Problem) Is(target error) bool {
if p == nil {
return target == nil
}
return errors.Is(p.cause, target)
}
func (p *Problem) MarshalJSON() ([]byte, error) {
if p == nil {
return []byte("null"), nil
}
if p.data == nil {
return []byte("{}"), nil
}
return json.Marshal(p.data)
}
func (p *Problem) UnmarshalJSON(b []byte) error {
if p == nil {
return errors.New("problem: UnmarshalJSON on nil *Problem")
}
if p.data == nil {
p.data = make(map[string]any)
}
for k := range p.data {
delete(p.data, k)
}
return json.Unmarshal(b, &p.data)
}
func (p *Problem) WriteHeaderTo(w http.ResponseWriter) {
w.Header().Set("Content-Type", MediaType)
if p == nil || p.data == nil {
return
}
if s, ok := p.data["status"]; ok {
if code, ok := asStatusCode(s); ok {
w.WriteHeader(code)
}
}
}
func (p *Problem) WriteTo(w http.ResponseWriter) (int, error) {
return p.write(w, http.StatusInternalServerError)
}
func (p *Problem) write(w http.ResponseWriter, fallback int) (int, error) {
w.Header().Set("Content-Type", MediaType)
code := fallback
if code == 0 {
code = http.StatusInternalServerError
}
if p != nil {
if s, ok := p.data["status"].(int); ok {
code = s
}
}
w.WriteHeader(code)
return w.Write(p.JSON())
}
func Wrap(err error) Option {
return optionFunc(func(p *Problem) { p.cause = err })
}
func WrapPublic(err error) Option {
return optionFunc(func(p *Problem) {
p.cause = err
ensureMap(p)
p.data["cause"] = err.Error()
})
}
func Type(uri string) Option {
return optionFunc(func(p *Problem) {
ensureMap(p)
p.data["type"] = uri
})
}
func Title(title string) Option {
return optionFunc(func(p *Problem) {
ensureMap(p)
p.data["title"] = title
})
}
func Status(status int) Option {
return optionFunc(func(p *Problem) {
ensureMap(p)
p.data["status"] = status
})
}
func Detail(detail string) Option {
return optionFunc(func(p *Problem) {
ensureMap(p)
p.data["detail"] = detail
})
}
func Instance(uri string) Option {
return optionFunc(func(p *Problem) {
ensureMap(p)
p.data["instance"] = uri
})
}
func Ext(key string, value any) Option {
return optionFunc(func(p *Problem) {
ensureMap(p)
p.data[key] = value
})
}
func Custom(key string, value any) Option { return Ext(key, value) }
func (p *Problem) With(key string, value any) *Problem {
return p.Append(Ext(key, value))
}
func ensureMap(p *Problem) {
if p.data == nil {
p.data = make(map[string]any)
}
}
func asStatusCode(v any) (int, bool) {
switch s := v.(type) {
case int:
return s, true
default:
return 0, false
}
}