forked from treeverse/lakeFS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_error.go
83 lines (71 loc) · 1.6 KB
/
map_error.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
package retention
import (
"context"
"fmt"
"sort"
"strings"
"github.com/treeverse/lakefs/logging"
)
// Fields is a string-keyed map with a nice printed representation.
type Fields map[string]interface{}
func (f Fields) String() string {
keys := make([]string, 0, len(f))
for k := range f {
keys = append(keys, k)
}
sort.Strings(keys)
var b strings.Builder
sep := ""
for _, k := range keys {
b.WriteString(fmt.Sprintf("%s%s=%v", sep, k, f[k]))
if sep == "" {
sep = ", "
}
}
return b.String()
}
func copyFields(f Fields) Fields {
ret := Fields{}
for k, v := range f {
ret[k] = v
}
return ret
}
// WithField augments fields with another field.
func (f Fields) WithField(key string, value interface{}) Fields {
ret := copyFields(f)
ret[key] = value
return ret
}
// WithFields merges two Fields.
func (f Fields) WithFields(g Fields) Fields {
ret := copyFields(f)
for k, v := range g {
ret[k] = v
}
return ret
}
// FromLoggerContext returns Fields using logging keys from ctx. This is not stealing: logging
// exports the field key.
func FromLoggerContext(ctx context.Context) Fields {
ret := Fields{}
loggerFields := ctx.Value(logging.LogFieldsContextKey)
if loggerFields != nil {
for k, v := range loggerFields.(logging.Fields) {
ret[k] = v
}
}
return ret
}
// MapError wraps an error and adds multiple keyed additional Fields of string-keyed
// information.
type MapError struct {
Fields Fields
WrappedError error
}
func (m MapError) Unwrap() error {
return m.WrappedError
}
func (m MapError) Error() string {
return fmt.Sprintf("%+v %s", m.Fields, m.WrappedError)
}