-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyael.go
48 lines (39 loc) · 974 Bytes
/
yael.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
package yael
// E defines the standard yael error type which conforms to errors.Wrapper
// and error interfaces
type E struct {
Code string `json:"code"`
Reason *E `json:"reason,omitempty"`
Meta map[string]interface{} `json:"meta,omitempty"`
sensitive bool
}
// New returns a new instance of E with code set
func New(code string) *E {
return &E{
Code: code,
}
}
// WithMeta sets key/value metadata on the error directly
func (e *E) WithMeta(key string, value interface{}) *E {
if e.Meta == nil {
e.Meta = make(map[string]interface{}, 1)
}
e.Meta[key] = value
return e
}
// WithReason sets the underlying error reason
func (e *E) WithReason(reason *E) *E {
e.Reason = reason
return e
}
// Error conforms to the standar error interface
func (e E) Error() string {
return e.Code
}
// Unwrap conforms to the errors.Wrapper interface
func (e *E) Unwrap() error {
if e == nil {
return nil
}
return e.Reason
}