-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem.go
More file actions
84 lines (73 loc) · 2.01 KB
/
problem.go
File metadata and controls
84 lines (73 loc) · 2.01 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
package hx
// Problem creates a ProblemDetails instance with the provided status and summary.
func Problem(status int, summary string, opts ...ProblemOpt) ProblemDetails {
pr := ProblemDetails{
StatusCode: status,
Title: summary,
}
for _, o := range opts {
o.applyProblemOpt(&pr)
}
return pr
}
// WithTypeURI sets the Type field of ProblemDetails.
// The type is a URI reference that identifies the problem type.
func WithTypeURI(s string) problemOpt {
return func(p *ProblemDetails) {
p.Type = s
}
}
// WithDetail sets the Detail field of ProblemDetails.
// The detail contains a human-readable explanation specific to this occurrence of the problem.
func WithDetail(s string) problemOpt {
return func(p *ProblemDetails) {
p.Detail = s
}
}
// Field represents a key-value pair that can be added to ProblemDetails extensions.
type Field struct {
Key string
Val any
}
// F is a shorthand constructor for creating Field instances.
func F(k string, v any) Field {
return Field{
Key: k,
Val: v,
}
}
// WithField adds a single field to the Extensions map of ProblemDetails.
// If Extensions is nil, it initializes a new map.
func WithField(f Field) problemOpt {
return func(p *ProblemDetails) {
if p.Extensions == nil {
p.Extensions = map[string]any{}
}
p.Extensions[f.Key] = f.Val
}
}
// WithFields sets multiple fields at once.
func WithFields(kv ...Field) problemOpt {
return func(p *ProblemDetails) {
if p.Extensions == nil {
p.Extensions = map[string]any{}
}
for _, f := range kv {
p.Extensions[f.Key] = f.Val
}
}
}
// WithInstance sets the Instance field of ProblemDetails.
// The instance is a URI reference that identifies the specific occurrence of the problem.
func WithInstance(s string) problemOpt {
return func(p *ProblemDetails) {
p.Instance = s
}
}
// WithCause sets the underlying error that caused this problem.
// This error will be logged but not included in the JSON response.
func WithCause(err error) problemOpt {
return func(p *ProblemDetails) {
p.Cause = err
}
}