forked from kataras/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.go
152 lines (127 loc) · 3.23 KB
/
reporter.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
package errors
import (
"sync"
)
// StackError contains the Stack method.
type StackError interface {
Stack() []Error
Error() string
}
// PrintAndReturnErrors prints the "err" to the given "printer",
// printer will be called multiple times if the "err" is a StackError, where it contains more than one error.
func PrintAndReturnErrors(err error, printer func(string, ...interface{})) error {
if err == nil || err.Error() == "" {
return nil
}
if stackErr, ok := err.(StackError); ok {
if len(stackErr.Stack()) == 0 {
return nil
}
stack := stackErr.Stack()
for _, e := range stack {
if e.HasStack() {
for _, es := range e.Stack {
printer("%v", es)
}
continue
}
printer("%v", e)
}
return stackErr
}
printer("%v", err)
return err
}
// Reporter is a helper structure which can
// stack errors and prints them to a printer of func(string).
type Reporter struct {
mu sync.Mutex
wrapper Error
}
// NewReporter returns a new empty error reporter.
func NewReporter() *Reporter {
return &Reporter{wrapper: New("")}
}
// AddErr adds an error to the error stack.
// if "err" is a StackError then
// each of these errors will be printed as individual.
//
// Returns true if this "err" is not nil and it's added to the reporter's stack.
func (r *Reporter) AddErr(err error) bool {
if err == nil {
return false
}
if stackErr, ok := err.(StackError); ok {
r.addStack(stackErr.Stack())
} else {
r.mu.Lock()
r.wrapper = r.wrapper.AppendErr(err)
r.mu.Unlock()
}
return true
}
// Add adds a formatted message as an error to the error stack.
//
// Returns true if this "err" is not nil and it's added to the reporter's stack.
func (r *Reporter) Add(format string, a ...interface{}) bool {
if format == "" && len(a) == 0 {
return false
}
// usually used as: "module: %v", err so
// check if the first argument is error and if that error is empty then don't add it.
if len(a) > 0 {
f := a[0]
if e, ok := f.(interface {
Error() string
}); ok {
if e.Error() == "" {
return false
}
}
}
r.mu.Lock()
r.wrapper = r.wrapper.Append(format, a...)
r.mu.Unlock()
return true
}
// Describe same as `Add` but if "err" is nil then it does nothing.
func (r *Reporter) Describe(format string, err error) {
if err == nil {
return
}
if stackErr, ok := err.(StackError); ok {
r.addStack(stackErr.Stack())
return
}
r.Add(format, err)
}
// PrintStack prints all the errors to the given "printer".
// Returns itself in order to be used as printer and return the full error in the same time.
func (r *Reporter) PrintStack(printer func(string, ...interface{})) error {
return PrintAndReturnErrors(r, printer)
}
// Stack returns the list of the errors in the stack.
func (r *Reporter) Stack() []Error {
return r.wrapper.Stack
}
func (r *Reporter) addStack(stack []Error) {
for _, e := range stack {
if e.Error() == "" {
continue
}
r.mu.Lock()
r.wrapper = r.wrapper.AppendErr(e)
r.mu.Unlock()
}
}
// Error implements the error, returns the full error string.
func (r *Reporter) Error() string {
return r.wrapper.Error()
}
// Return returns nil if the error is empty, otherwise returns the full error.
func (r *Reporter) Return() error {
if r.Error() == "" {
return nil
}
return r
}