-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrors.go
149 lines (127 loc) · 3.21 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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package errutil
import (
"errors"
"fmt"
"strings"
)
// Wrap wraps an error with the ability to add multiple behavior to that error
// e.g.
//
// Wrap(errors.New("my error"), WithAccessDenied(true), WithRateLimit(true))
func Wrap(err error, opts ...OptsFunc) error {
if err == nil {
return nil
}
e := multiKindErr{
opts: ToOpts(opts...),
error: err,
}
if len(e.opts.Tags) > 0 {
e.error = NewTagged(err, e.opts.Tags...)
}
if e.opts.StatusCode != nil {
e.error = NewStatusCode(e.error, *e.opts.StatusCode)
}
if e.opts.InternalErrorMessage != nil {
e.error = NewInternalErrorMessage(e.error, *e.opts.InternalErrorMessage)
}
if e.opts.Code != nil {
e.error = NewCode(e.error, *e.opts.Code)
}
if e.opts.StackTrace != nil {
e.error = NewStacked(e.error, *e.opts.StackTrace+1)
} else {
e.error = NewStacked(e.error, 1)
}
return e
}
// New creates a new error with the ability to add multiple behavior to that error
// e.g.
//
// New("my error", WithAccessDenied(true), WithRateLimit(true))
func New(message string, opts ...OptsFunc) error {
info := ToOpts(opts...)
if info.StackTrace != nil {
opts = append(opts, WithStackTrace(*info.StackTrace+1))
} else {
opts = append(opts, WithStackTrace(1))
}
return Wrap(errors.New(message), opts...)
}
var (
_ AccessDenier = (*multiKindErr)(nil)
_ Conflicter = (*multiKindErr)(nil)
_ Exister = (*multiKindErr)(nil)
_ NotFounder = (*multiKindErr)(nil)
_ RateLimiter = (*multiKindErr)(nil)
_ TooLarge = (*multiKindErr)(nil)
_ TooManyRequester = (*multiKindErr)(nil)
_ Temporarily = (*multiKindErr)(nil)
_ Unauthorizable = (*multiKindErr)(nil)
_ error = (*multiKindErr)(nil)
)
type Opts struct {
//::builder-gen -no-builder -with-globals -prefix=With -with-optional-bools
AccessDenied bool
Conflict bool
NotFound bool
Exists bool
TooLarge bool
RateLimit bool
Tags []Tag
StackTrace *int
Unauthorized bool
Temporary bool
InternalErrorMessage *string
Code *string
StatusCode *int
TooManyRequests bool
}
type multiKindErr struct {
opts Opts
error
}
func (m multiKindErr) AccessDenied() bool {
return m.opts.AccessDenied
}
func (m multiKindErr) Conflict() bool {
return m.opts.Conflict
}
func (m multiKindErr) Exists() bool {
return m.opts.Exists
}
func (m multiKindErr) Format(s fmt.State, verb rune) {
if formatter, ok := m.error.(fmt.Formatter); ok {
formatter.Format(s, verb)
return
} else {
var builder strings.Builder
builder.WriteString("%")
if s.Flag('+') {
builder.WriteRune('+')
}
builder.WriteRune(verb)
fmt.Fprintf(s, builder.String(), m.error)
}
}
func (m multiKindErr) NotFound() bool {
return m.opts.NotFound
}
func (m multiKindErr) RateLimit() bool {
return m.opts.RateLimit
}
func (m multiKindErr) Temporary() bool {
return m.opts.Temporary
}
func (m multiKindErr) TooLarge() bool {
return m.opts.TooLarge
}
func (m multiKindErr) TooManyRequests() bool {
return m.opts.TooManyRequests
}
func (m multiKindErr) Unauthorized() bool {
return m.opts.Unauthorized
}
func (m multiKindErr) Unwrap() error {
return m.error
}