-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuilder.go
107 lines (81 loc) · 1.84 KB
/
builder.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
package failure
import (
"fmt"
)
// Builder helps build an error
type Builder interface {
WithField(name string, value interface{}) Builder
WithFields(fields Fields) Builder
WithMessage(message string) Builder
WithMessagef(format string, a ...interface{}) Builder
ParentOf(inner error) Builder
Done() error
}
// Impersonator gives the ability to work with external error types
// that implement this interface.
type Impersonator interface {
error
Impersonate(b Builder)
}
type builder struct {
n *node
}
// Done returns the new built error
func (b *builder) Done() error {
return b.n
}
// WithField adds a field named name with the value value to
// the error
func (b *builder) WithField(name string, value interface{}) Builder {
if name == "" {
return b
}
if name == MessageField {
b.n.Message = fmt.Sprintf("%v", value)
return b
}
if b.n.Fields == nil {
b.n.Fields = make(Fields)
}
b.n.Fields[name] = value
return b
}
// WithFields adds fields to the error
func (b *builder) WithFields(fields Fields) Builder {
if fields == nil || len(fields) == 0 {
return b
}
for k, v := range fields {
if k == "" {
continue
}
if k == MessageField {
b.n.Message = fmt.Sprintf("%v", v)
} else {
if b.n.Fields == nil {
b.n.Fields = make(Fields)
}
b.n.Fields[k] = v
}
}
return b
}
// ParentOf sets the new error to be the parent of inner
func (b *builder) ParentOf(inner error) Builder {
var n *node
if inner != nil {
n = impersonate(inner)
}
b.n.Inner = n
return b
}
// WithMessage sets the error's message
func (b *builder) WithMessage(message string) Builder {
b.n.Message = message
return b
}
// WithMessagef formats the error's message according to
// a format specifier
func (b *builder) WithMessagef(format string, a ...interface{}) Builder {
return b.WithMessage(fmt.Sprintf(format, a...))
}