-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
58 lines (50 loc) · 1.5 KB
/
options.go
File metadata and controls
58 lines (50 loc) · 1.5 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
package problem
import "fmt"
// Option is a functional option for customizing a Problem details object.
type Option func(*Problem)
// WithDetail sets the human-readable explanation specific to this occurrence of the problem.
func WithDetail(detail string) Option {
return func(p *Problem) {
p.Detail = detail
}
}
// WithDetailf formats and sets the detail message.
func WithDetailf(format string, args ...any) Option {
return func(p *Problem) {
p.Detail = fmt.Sprintf(format, args...)
}
}
// WithInstance sets the URI reference that identifies the specific occurrence of the problem.
func WithInstance(instance string) Option {
return func(p *Problem) {
p.Instance = instance
}
}
// WithExtension sets a single extension field.
// According to RFC 7807, extension members are additional members within the Problem Details object.
func WithExtension(key string, value any) Option {
return func(p *Problem) {
if p.Extensions == nil {
p.Extensions = make(map[string]any)
}
p.Extensions[key] = value
}
}
// WithExtensions merges multiple extension fields at once.
func WithExtensions(ext map[string]any) Option {
return func(p *Problem) {
if p.Extensions == nil {
p.Extensions = make(map[string]any)
}
for k, v := range ext {
p.Extensions[k] = v
}
}
}
// WithErr sets the underlying error that caused this problem.
// This allows the Problem to wrap another error, supporting standard Go wrapping (errors.Is/As).
func WithErr(err error) Option {
return func(p *Problem) {
p.err = err
}
}