-
Notifications
You must be signed in to change notification settings - Fork 4
/
marshaller_error.go
212 lines (194 loc) · 4.74 KB
/
marshaller_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
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
202
203
204
205
206
207
208
209
210
211
212
package logfmt
import (
"errors"
"strings"
"github.com/insolar/assured-ledger/ledger-core/vanilla/throw"
)
type errorMarshaller struct {
reports []errorReport
stack throw.StackTrace
msg string
severity throw.Severity
hasPanic bool
}
type errorReport struct {
marshaller LogObjectMarshaller
strValue string
stack throw.StackTrace
}
func (v *errorMarshaller) appendReport(m LogObjectMarshaller, s string, ps *string, st throw.StackTrace) bool {
switch {
case ps == nil:
//
case s == "":
s = *ps
case s != "":
s2 := *ps
if s2 != "" {
s += "\t" + s2
}
}
if m != nil || s != "" {
v.reports = append(v.reports, errorReport{m, s, st})
return true
}
if st != nil {
v.reports = append(v.reports, errorReport{m, s, st})
}
return false
}
func (v *errorMarshaller) appendError(mf MarshallerFactory, x interface{}, st throw.StackTrace) bool {
m, ps := fmtLogStruct(x, mf, true, true)
return v.appendReport(m, "", ps, st)
}
func (v *errorMarshaller) appendExtraInfo(mf MarshallerFactory, x interface{}) bool {
if extras, severity, extra, ok := throw.UnwrapExtraInfo(x); ok {
if v.severity == 0 && severity != 0 {
v.severity = severity
}
m, ps := fmtLogStruct(extra, mf, false, true)
if v.appendReport(m, extras, ps, nil) {
return true
}
if err, ok := extra.(error); ok {
return v.appendReport(nil, err.Error(), nil, nil)
}
}
return false
}
func (v *errorMarshaller) fillLevels(errChain error, mf MarshallerFactory) bool {
if errChain == nil {
return false
}
hasStacks := false
isSupersededTrace := false
throw.Walk(errChain, func(err error, holder throw.StackTraceHolder) bool {
var localSt throw.StackTrace
if holder != nil {
if st, stMode := holder.DeepestStackTrace(); st != nil {
if !isSupersededTrace {
hasStacks = true
localSt = st
}
switch {
case stMode == throw.SupersededTrace:
isSupersededTrace = true
case stMode == throw.InheritedTrace:
localSt = nil
default:
isSupersededTrace = false
}
}
//v.appendError(mf, true, holder)
//v.appendExtraInfo(mf, holder)
if vv, ok := holder.(throw.PanicHolder); ok {
v.hasPanic = true
r := vv.Recovered()
if _, ok := r.(error); !ok {
v.appendError(mf, r, nil)
}
}
err = holder.Cause()
}
if err == nil {
return false
}
hasReport := v.appendError(mf, err, localSt)
if !v.appendExtraInfo(mf, err) && !hasReport {
if s := err.Error(); s != "" {
if wrapped := errors.Unwrap(err); wrapped != nil {
s, _ = cutOut(s, wrapped.Error())
}
v.reports = append(v.reports, errorReport{nil, s, nil})
}
}
return false
})
switch {
case len(v.reports) > 1:
for i, e := range v.reports {
if v.msg == "" && e.strValue != "" {
v.msg = e.strValue
v.reports[i].strValue = ""
if v.stack != nil {
break
}
}
if v.stack == nil && e.stack != nil && e.stack.IsFullStack() {
v.stack = e.stack
v.reports[i].stack = nil
if v.msg != "" {
break
}
}
}
case len(v.reports) == 0:
// very strange ...
if ls, ok := errChain.(LogStringer); ok {
v.msg = ls.LogString()
} else {
v.msg = errChain.Error()
}
if hasStacks {
v.msg = throw.TrimStackTrace(v.msg)
}
default:
v.msg = v.reports[0].strValue
v.stack = v.reports[0].stack
if v.reports[0].marshaller == nil {
v.reports = nil
} else {
v.reports[0].stack = nil
v.reports[0].strValue = ""
}
}
return true
}
func cutOut(s, substr string) (string, bool) {
if substr == "" {
return s, false
}
if i := strings.LastIndex(s, substr); i >= 0 {
s0 := s[:i]
s1 := s[i+len(substr):]
if s0t := strings.TrimSpace(s0); s0t != "" {
return s0 + "%w" + s1, true
}
if s1t := strings.TrimSpace(s1); s1t != "" {
return "%w" + s1, true
}
return "", true
}
return s, false
}
func (v errorMarshaller) MarshalLogObject(output LogObjectWriter, collector LogObjectMetricCollector) (string, bool) {
for i := len(v.reports) - 1; i >= 0; i-- {
el := v.reports[i]
if el.marshaller != nil {
if s, defMessage := el.marshaller.MarshalLogObject(output, collector); !defMessage && s != "" {
output.AddErrorField(s, el.stack, 0, false)
el.stack = nil
}
}
if s := el.strValue; s != "" || el.stack != nil {
output.AddErrorField(s, el.stack, 0, false)
}
}
if v.stack != nil || v.severity != 0 {
output.AddErrorField("", v.stack, v.severity, v.hasPanic)
}
return v.msg, false
}
func (v errorMarshaller) MarshalMutedLogObject(collector LogObjectMetricCollector) {
if collector == nil {
return
}
for i := len(v.reports) - 1; i >= 0; i-- {
if mm, ok := v.reports[i].marshaller.(MutedLogObjectMarshaller); ok {
mm.MarshalMutedLogObject(collector)
}
}
}
func (v errorMarshaller) IsEmpty() bool {
return len(v.reports) == 0
}