-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapping.go
131 lines (116 loc) · 3 KB
/
wrapping.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package errors
import (
"fmt"
"strings"
errors "github.com/pkg/errors"
)
// WithStack annotates err with a stack trace at the point WithStack was called.
// If err is nil, WithStack returns nil.
func WithStack(err error) error {
return errors.WithStack(err)
}
// Wrap returns an error annotating err with a stack trace
// at the point Wrap is called, and the supplied message.
// If err is nil, Wrap returns nil.
func Wrap(err error, message string) error {
return errors.Wrap(err, message)
}
// Wrapf returns an error annotating err with a stack trace
// at the point Wrapf is call, and the format specifier.
// If err is nil, Wrapf returns nil.
func Wrapf(err error, format string, args ...interface{}) error {
return errors.Wrapf(err, format, args...)
}
// WithMessage annotates err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessage(err error, message string) error {
return errors.WithMessage(err, message)
}
// WithMessagef formats an err with a new message.
// If err is nil, WithMessage returns nil.
func WithMessagef(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
msg := fmt.Sprintf(format, args...)
return errors.WithMessage(err, msg)
}
type withOp struct {
op Op
cause error
}
func (err *withOp) Error() string {
b := new(strings.Builder)
if err.op != "" {
pad(b, separator)
b.WriteString(string(err.op))
}
if err.cause != nil {
pad(b, separator)
b.WriteString(err.cause.Error())
}
if b.Len() == 0 {
return "no error"
}
return b.String()
}
func (err *withOp) Cause() error {
return err.cause
}
func (err *withOp) Op() Op {
return err.op
}
// WithOp returns an error annotating err with a hint of the operation name
// at the point WithOp is called. If err is nil, WithOp returns nil.
func WithOp(err error, op Op) error {
if err == nil {
return nil
}
if op == "" {
return err
}
return &withOp{
cause: err,
op: op,
}
}
// WithOpf returns an error annotating err with a hint of the operation name
// at the point WithOpf is call, and the format specifier.
// If err is nil, WithOpf returns nil.
func WithOpf(err error, op Op, format string, args ...interface{}) error {
if err == nil {
return nil
}
if op == "" {
return err
}
return &withOp{
cause: errors.WithMessage(err, fmt.Sprintf(format, args...)),
op: op,
}
}
// WithKind returns an error annotating err with the service specific kind of err
// at the point WithKind is called. If err is nil, WithKind returns nil.
func WithKind(err error, kind Kind, msg string) error {
if err == nil {
return nil
}
return &Error{
cause: err,
msg: msg,
kind: kind,
}
}
// WithKindf returns an error annotating err with the service specific kind of err
// at the point WithKindf is call, and the format specifier.
// If err is nil, WithKindf returns nil.
func WithKindf(err error, kind Kind, format string, args ...interface{}) error {
if err == nil {
return nil
}
return &Error{
cause: err,
msg: fmt.Sprintf(format, args...),
kind: kind,
}
}