-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
66 lines (53 loc) · 1.13 KB
/
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
package stacktrace
import (
"fmt"
"strings"
)
type Error struct {
wrapped error
cause error
message string
frames []frame
}
func (e *Error) Error() string {
var sb strings.Builder
sb.WriteString("Caused by: ")
sb.WriteString(e.message)
if _, ok := e.cause.(*Error); !ok {
if e.cause != nil {
sb.WriteString(" <-- ")
sb.WriteString(e.cause.Error())
}
}
sb.WriteString("\n")
for _, frame := range e.frames {
if frame.file != "" && frame.line != 0 {
sb.WriteString(" at ")
sb.WriteString(frame.file)
sb.WriteString(":")
sb.WriteString(fmt.Sprint(frame.line))
if frame.function != "" {
sb.WriteString(" (")
sb.WriteString(frame.function)
sb.WriteString(")")
}
sb.WriteString("\n")
}
}
if _, ok := e.cause.(*Error); ok {
buf := sb.String()
sb.Reset()
sb.WriteString(buf)
sb.WriteString("\n")
sb.WriteString(e.cause.Error())
}
return sb.String()
}
func (e *Error) Wrapped() error {
if err, ok := e.cause.(*Error); ok {
e.wrapped = fmt.Errorf("%v: %v", e.message, err.Wrapped())
} else {
e.wrapped = fmt.Errorf("%v: %v", e.message, e.cause)
}
return e.wrapped
}