-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror_serialization.go
More file actions
201 lines (168 loc) · 4.89 KB
/
Copy patherror_serialization.go
File metadata and controls
201 lines (168 loc) · 4.89 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package errors
import (
"fmt"
"strconv"
"strings"
"github.com/goccy/go-json"
"github.com/pixie-sh/logger-go/env"
)
// MarshalJSON implement json marshaller interface
func (ec ErrorCode) MarshalJSON() ([]byte, error) {
c := ec.String()
return json.Marshal(&c)
}
// UnmarshalJSON implement json marshaller interface
func (ec *ErrorCode) UnmarshalJSON(data []byte) error {
var mErr string
err := json.Unmarshal(data, &mErr)
if err != nil {
return err
}
codeParts := strings.Split(mErr, "-")
if len(codeParts) != 2 {
Logger.Warn("unable to parse error code for %s. using default %v", mErr, GenericErrorCode)
ec.Name = GenericErrorCode.Name
ec.Value = GenericErrorCode.Value
ec.HTTPError = GenericErrorCode.HTTPError
return nil
}
value, err := strconv.ParseInt(codeParts[1], 10, 64)
if err != nil {
Logger.Warn("unable to parse error code for %s. using default %v", mErr, GenericErrorCode)
ec.Name = GenericErrorCode.Name
ec.Value = GenericErrorCode.Value
ec.HTTPError = GenericErrorCode.HTTPError
return nil
}
ec.Name = codeParts[0]
ec.Value = int(value)
ec.HTTPError = ec.Value % 1000
if ec.HTTPError < 0 {
// Ensure ec.HTTPError always represents the last three digits of ec.Value,
// even when ec.Value is negative
ec.HTTPError += 1000
}
return nil
}
func (ec ErrorCode) String() string {
return fmt.Sprintf("%s-%d", ec.Name, ec.Value)
}
// Error implements the error interface
func (e Error) Error() string {
var ecStr = e.Code.String() + " "
if e.Code == GenericErrorCode || e.Code == UnknownErrorCode {
ecStr = ""
}
if len(e.NestedError) == 0 {
return fmt.Sprintf("%s%s", ecStr, e.Message)
}
errStr := strings.Builder{}
errStr.WriteString(fmt.Sprintf("%s%s", ecStr, e.Message))
for _, err := range e.NestedError {
errStr.WriteString(fmt.Sprintf("; %s", err.Error()))
}
return errStr.String()
}
// String implements Stringer interface
func (e Error) String() string {
return e.Error()
}
// Unwrap only returns the first NestedError
// which is the most common use case
func (e Error) Unwrap() error {
if len(e.NestedError) == 0 {
return nil
}
return e.NestedError[0]
}
// Is reports whether any error in e's chain has an ErrorCode matching target's.
// It implements the optional errors.Is interface from the standard library, so
// stdlib errors.Is(err, target) will match by ErrorCode across the full nested
// tree (including JoinedErrorCode containers).
//
// Returns false if target is nil or not an *Error. For non-*Error targets the
// standard library continues traversal via Unwrap.
func (e *Error) Is(target error) bool {
if target == nil {
return false
}
t, ok := As(target)
if !ok {
return false
}
_, found := Has(e, t.Code, true)
return found
}
func (e Error) MarshalJSON() ([]byte, error) {
// Create a custom type for marshaling that won't trigger the MarshalJSON method recursively
type AliasError struct {
Code ErrorCode `json:"code,omitempty"`
Message string `json:"message,omitempty"`
NestedError []json.RawMessage `json:"nested_error,omitempty"`
Trace *StackTrace `json:"stack_trace,omitempty"`
FieldErrors []*FieldError `json:"field_errors,omitempty"`
}
aliasErr := AliasError{
Code: e.Code,
Message: e.Message,
FieldErrors: e.FieldErrors,
}
if env.IsDebugActive() {
aliasErr.Trace = e.Trace
}
if len(e.NestedError) > 0 {
aliasErr.NestedError = make([]json.RawMessage, len(e.NestedError))
for i, nested := range e.NestedError {
if nested == nil {
continue
}
if customErr, ok := As(nested); ok {
data, err := json.Marshal(customErr)
if err != nil {
return nil, err
}
aliasErr.NestedError[i] = data
} else {
errStr := nested.Error()
data, err := json.Marshal(errStr)
if err != nil {
return nil, err
}
aliasErr.NestedError[i] = data
}
}
}
return json.Marshal(aliasErr)
}
func (e *Error) UnmarshalJSON(data []byte) error {
type AliasError struct {
Code ErrorCode `json:"code,omitempty"`
Message string `json:"message,omitempty"`
NestedError []json.RawMessage `json:"nested_error,omitempty"`
Trace *StackTrace `json:"stack_trace,omitempty"`
FieldErrors []*FieldError `json:"field_errors,omitempty"`
}
var aliasErr AliasError
if err := json.Unmarshal(data, &aliasErr); err != nil {
return err
}
e.Code = aliasErr.Code
e.Message = aliasErr.Message
e.FieldErrors = aliasErr.FieldErrors
e.Trace = aliasErr.Trace
if len(aliasErr.NestedError) > 0 {
e.NestedError = make([]error, len(aliasErr.NestedError))
for i, nestedData := range aliasErr.NestedError {
var customErr Error
if err := json.Unmarshal(nestedData, &customErr); err == nil {
e.NestedError[i] = &customErr
continue
}
var errStr string
if err := json.Unmarshal(nestedData, &errStr); err == nil {
e.NestedError[i] = fmt.Errorf("%s", errStr)
}
}
}
return nil
}