-
Notifications
You must be signed in to change notification settings - Fork 1
/
stack.go
69 lines (58 loc) · 1.28 KB
/
stack.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
package errors
import (
"errors"
"fmt"
"io"
"github.com/mailgun/errors/callstack"
)
// Stack annotates err with a stack trace at the point Stack was called.
// If err is nil, Stack returns nil.
func Stack(err error) error {
if err == nil {
return nil
}
return &stack{
err,
callstack.New(1),
}
}
type stack struct {
error
*callstack.CallStack
}
func (w *stack) Unwrap() error { return w.error }
func (w *stack) Is(target error) bool {
_, ok := target.(*stack)
return ok
}
// Cause returns the wrapped error which was the original
// cause of the issue. We only support this because some code
// depends on github.com/pkg/errors.Cause() returning the cause
// of the error.
// Deprecated: use error.Is() or error.As() instead
func (w *stack) Cause() error { return w.error }
func (w *stack) HasFields() map[string]any {
if child, ok := w.error.(HasFields); ok {
return child.HasFields()
}
var f HasFields
if errors.As(w.error, &f) {
return f.HasFields()
}
return nil
}
func (w *stack) Format(s fmt.State, verb rune) {
switch verb {
case 'v':
if s.Flag('+') {
_, _ = fmt.Fprintf(s, "%+v", w.Unwrap())
w.CallStack.Format(s, verb)
return
}
fallthrough
case 's':
_, _ = io.WriteString(s, w.Error())
case 'q':
_, _ = fmt.Fprintf(s, "%q", w.Error())
}
}