-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparams.go
99 lines (80 loc) · 2.26 KB
/
params.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
package erk
import (
"encoding/json"
"errors"
"strings"
)
// Paramable errors that support appending Params and getting Params.
type Paramable interface {
WithParams(params Params) error
Params() Params
}
// OriginalErrorParam is the param key that contains the wrapped error.
//
// This allows the original error to be used in message templates.
// Also, errors can be unwrapped, using errors.Unwrap(err).
const OriginalErrorParam = "err"
// Params are key value parameters that are usuable in the message template.
type Params map[string]interface{}
// WithParams adds parameters to an error.
//
// If err does not satisfy Paramable, the original error is returned.
// A nil param value deletes the param key.
func WithParams(err error, params Params) error {
if len(params) == 0 {
return err
}
var p Paramable
if errors.As(err, &p) {
return p.WithParams(params)
}
return err
}
// WithParam adds a parameter to an error.
//
// If err does not satisfy Paramable, the original error is returned.
// A nil param value deletes the param key.
func WithParam(err error, key string, value interface{}) error {
return WithParams(err, Params{key: value})
}
// GetParams returns the error's parameters.
//
// If err does not satisfy Paramable, nil is returned.
func GetParams(err error) Params {
var p Paramable
if errors.As(err, &p) {
return p.Params()
}
return nil
}
// Clone the params into a copy.
func (p Params) Clone() Params {
if p == nil {
return Params{}
}
paramsCopy := Params{}
for k, v := range p {
paramsCopy[k] = v
}
return paramsCopy
}
// MarshalJSON by converting the "err" element to a string.
func (p Params) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}(p.prep(IndentSpaces)))
}
func (p Params) prep(indentLevel string) Params {
p2 := p.Clone()
if rawErr, ok := p2[OriginalErrorParam]; ok {
if indentable, ok := rawErr.(ErrorIndentable); ok {
p2[OriginalErrorParam] = indentable.IndentError(indentLevel)
} else if err, ok := rawErr.(error); ok {
strError := err.Error()
if strings.Contains(strError, "\n") {
strError = strings.ReplaceAll(err.Error(), "\n", "\n"+indentLevel)
strError = "\n" + indentLevel + strError // Add a leading newline
}
p2[OriginalErrorParam] = strError
}
}
return p2
}