forked from pkg/errors
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom.go
111 lines (97 loc) · 1.83 KB
/
custom.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
package errors
import "fmt"
type ApiConfig struct {
CallerSkip int
}
type errorsApi struct {
cfg ApiConfig
}
func NewErrorsApi(cfg ApiConfig) *errorsApi {
return &errorsApi{
cfg: cfg,
}
}
var globalErrorsApi = NewErrorsApi(ApiConfig{
CallerSkip: 2,
})
func (e *errorsApi) New(message string) error {
return &fundamental{
msg: message,
stack: callers(e.cfg.CallerSkip),
}
}
func (e *errorsApi) Errorf(format string, args ...interface{}) error {
return &fundamental{
msg: fmt.Sprintf(format, args...),
stack: callers(e.cfg.CallerSkip),
}
}
func (e *errorsApi) WithStack(err error) error {
if err == nil {
return nil
}
return &withStack{
withMessage{
cause: err,
msg: "",
},
callers(e.cfg.CallerSkip),
}
}
func (e *errorsApi) Wrap(err error, message string) error {
if err == nil {
return nil
}
return &withStack{
withMessage{
cause: err,
msg: message,
},
callers(e.cfg.CallerSkip),
}
}
func (e *errorsApi) Wrapf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &withStack{
withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
},
callers(e.cfg.CallerSkip),
}
}
func (e *errorsApi) WithMessage(err error, message string) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: message,
}
}
func (e *errorsApi) WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &withMessage{
cause: err,
msg: fmt.Sprintf(format, args...),
}
}
func (e *errorsApi) WithDetails(err error, details ...any) error {
if err == nil {
return nil
}
return &withDetails{
cause: err,
details: details,
}
}
func (e *errorsApi) Details(err error) ([]any, bool) {
if w, ok := err.(*withDetails); ok {
return w.details, true
}
return nil, false
}