forked from creasty/apperrors
-
Notifications
You must be signed in to change notification settings - Fork 1
/
annotators.go
63 lines (52 loc) · 1.49 KB
/
annotators.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
package fail
import "fmt"
// Annotator is a function that annotates an error with information
type Annotator func(*Error)
// WithMessage annotates an error with the message
func WithMessage(msg string) Annotator {
return func(err *Error) {
if msg == "" {
return
}
err.Messages = append([]string{msg}, err.Messages...)
}
}
// WithMessagef annotates an error with the formatted message
func WithMessagef(msg string, args ...interface{}) Annotator {
return WithMessage(fmt.Sprintf(msg, args...))
}
// WithCode annotates an error with the code
func WithCode(code interface{}) Annotator {
return func(err *Error) {
err.Code = code
}
}
// WithIgnorable annotates an error with the reportability
func WithIgnorable() Annotator {
return func(err *Error) {
err.Ignorable = true
}
}
// WithTags annotates an error with tags
func WithTags(tags ...string) Annotator {
return func(err *Error) {
err.Tags = append(err.Tags, tags...)
}
}
// WithParam annotates an error with a key-value pair
func WithParam(key string, value interface{}) Annotator {
return WithParams(H{key: value})
}
// WithParams annotates an error with key-value pairs
func WithParams(h H) Annotator {
return func(err *Error) {
err.Params = err.Params.Merge(h)
}
}
// withStackTrace annotates an error with the stack trace from the point it was called
func withStackTrace(offset int) Annotator {
stackTrace := newStackTrace(offset + 1)
return func(err *Error) {
err.StackTrace = mergeStackTraces(err.StackTrace, stackTrace)
}
}