-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaugmented.go
54 lines (46 loc) · 1.18 KB
/
augmented.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
package exterror
import (
"fmt"
"sort"
)
// -----------------------------------------------------------------------------
// AugmentedError is just an error with extended data.
type AugmentedError struct {
Message string
Fields map[string]interface{}
Err error // Underlying error that occurred during the operation.
}
// -----------------------------------------------------------------------------
// NewAugmentedError creates a new AugmentedError.
func NewAugmentedError(wrappedErr error, text string, fields map[string]interface{}) *AugmentedError {
e := AugmentedError{}
e.Message = text
e.Fields = fields
e.Err = wrappedErr
return &e
}
// Unwrap returns the underlying error.
func (e *AugmentedError) Unwrap() error {
return e.Err
}
// Error returns a string representation of the error.
func (e *AugmentedError) Error() string {
if e == nil {
return ""
}
s := e.Message
if e.Fields != nil {
keys := make([]string, 0, len(e.Fields))
for k := range e.Fields {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
s += fmt.Sprintf(" [%s=%v]", k, e.Fields[k])
}
}
if e.Err != nil {
s += " [err=" + e.Err.Error() + "]"
}
return s
}