-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (59 loc) · 1.36 KB
/
main.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
package errs
import (
"fmt"
"runtime"
"strings"
)
func NewFull(code int, err error, format string, a ...any) *Error {
return &Error{Code: code, err: err, msg: fmt.Sprintf(format, a...), Stack: CallStack(3, 30)}
}
func NewMsg(code int, format string, a ...any) *Error {
return &Error{Code: code, msg: fmt.Sprintf(format, a...), Stack: CallStack(3, 30)}
}
func New(code int, err error) *Error {
return &Error{Code: code, err: err, Stack: CallStack(3, 30)}
}
func (e *Error) Short() string {
if e == nil {
return ""
}
return fmt.Sprintf("[%d] %s", e.Code, e.Message())
}
func (e *Error) Error() string {
if e == nil {
return ""
}
return fmt.Sprintf("[%d] %s\n%s", e.Code, e.Message(), e.Stack)
}
func (e *Error) Message() string {
if e.err == nil {
return e.msg
}
var errMsg string
if PrintErr != nil {
errMsg = PrintErr(e.err)
} else {
errMsg = e.err.Error()
}
if e.msg == "" {
return errMsg
}
return fmt.Sprintf("%s %s", e.msg, errMsg)
}
func (e *Error) Unwrap() error {
return e.err
}
func CallStack(skip, maxNum int) string {
pc := make([]uintptr, maxNum)
n := runtime.Callers(skip, pc)
frames := runtime.CallersFrames(pc[:n])
var texts = make([]string, 0, 16)
for {
f, more := frames.Next()
texts = append(texts, fmt.Sprintf(" at %v:%v %v", f.File, f.Line, f.Function))
if !more {
break
}
}
return strings.Join(texts, "\n")
}