-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
97 lines (82 loc) · 2.13 KB
/
errors.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
package errors
import (
"bytes"
"fmt"
"log/slog"
"github.com/go-errors/errors"
"github.com/jgolang/errors/codes"
)
// Error is a wrapper of an existing error containing the error stack trace at the moment of creation
type Error struct {
Wrapper *errors.Error
Message string // A non-technical, user-friendly message describing the error.
cause error
Code codes.Coder // A custom error code to categorize or identify the error.
}
// StackTrace Returns an string containing the stack trace computed at the creation moment of this `Error`.
func (err *Error) StackTraceStr() string {
frames := err.Wrapper.StackFrames()
buffer := bytes.NewBufferString("")
if err.Message != "" {
buffer.WriteString(fmt.Sprintf("\n\n%s\n\n", err.Message))
buffer.WriteString(fmt.Sprintf("· Cause: %s\n\n", err.Wrapper.Error()))
} else if err.Wrapper != nil {
buffer.WriteString(fmt.Sprintf("\n\n%s\n\n", err.Wrapper.Error()))
} else {
buffer.WriteString("\n\n")
}
for _, frame := range frames {
buffer.WriteString(
fmt.Sprintf(
"%s.%s\n",
frame.Package,
frame.Name,
),
)
buffer.WriteString(
fmt.Sprintf(
"· %s:%d\n",
frame.File,
frame.LineNumber,
),
)
}
return buffer.String()
}
func (err *Error) StackTrace() slog.Value {
frames := err.Wrapper.StackFrames()
var as []slog.Attr
for level, frame := range frames {
fmtFrame := fmt.Sprintf(
"%s:%d (%s)",
frame.File,
frame.LineNumber,
frame.Name,
)
as = append(as, slog.String(fmt.Sprintf("frame_%v", level), fmtFrame))
}
return slog.GroupValue(as...)
}
// Error returns the text of this `Error`
func (err *Error) Error() string {
var result string
// Include the custom error code if it exists
if err.Code != nil {
result += fmt.Sprintf("[%s]", err.Code.Str())
if err.Code.Msg() != "" {
result += fmt.Sprintf("(%s)", err.Code.Msg())
}
}
// Include the custom message if it exists
if err.Message != "" {
result += fmt.Sprintf(" %s", err.Message)
}
// Always include the original wrapped error message
if err.Wrapper != nil {
if result != "" {
result += ": "
}
result += err.Wrapper.Error()
}
return result
}