forked from BTrDB/btrdb-server
/
errors.go
249 lines (200 loc) · 5.28 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package bte
import (
"fmt"
"os"
"runtime/debug"
"context"
)
// This defines all the errors that BTrDB can throw
type bTE struct {
code int
reason string
cause error
}
type BTE interface {
error
Code() int
Reason() string
Cause() error
}
func (bte *bTE) Code() int {
return bte.code
}
func (bte *bTE) Reason() string {
return bte.reason
}
func (bte *bTE) Cause() error {
return bte.cause
}
func (bte *bTE) WrappedErrors() []error {
return []error{bte.cause}
}
func (bte *bTE) Error() string {
if bte.cause == nil {
return fmt.Sprintf("(%d: %s)", bte.code, bte.reason)
}
return fmt.Sprintf("(%d: %s because %s)", bte.code, bte.reason, bte.cause.Error())
}
func MaybeWrap(err error) BTE {
bt, ok := err.(BTE)
if ok {
return bt
}
return Err(GenericError, err.Error())
}
// Error codes:
// 400+ normal user errors
// 500+ abnormal errors that sysadmin should be notified about
func Err(code int, reason string) BTE {
if code >= 500 {
fmt.Fprintf(os.Stderr, "\n\n=== %d code error ===\nreason: %s\n", code, reason)
debug.PrintStack()
fmt.Fprintf(os.Stderr, "====\n\n")
}
return &bTE{
code: code,
reason: reason,
cause: nil,
}
}
func ErrF(code int, reasonz string, args ...interface{}) BTE {
reason := fmt.Sprintf(reasonz, args...)
if code >= 500 {
fmt.Fprintf(os.Stderr, "\n\n=== %d code error ===\nreason: %s\n", code, reason)
debug.PrintStack()
fmt.Fprintf(os.Stderr, "====\n\n")
}
return &bTE{
code: code,
reason: reason,
cause: nil,
}
}
func ErrW(code int, reason string, cause error) BTE {
if code >= 500 {
fmt.Fprintf(os.Stderr, "\n\n=== %d code error ===\nreason: %s\nbecause: %s", code, reason, cause.Error())
debug.PrintStack()
fmt.Fprintf(os.Stderr, "====\n\n")
}
scause := "<nil>"
if cause != nil {
scause = cause.Error()
}
return &bTE{
code: code,
reason: fmt.Sprintf("%s (%s)", reason, scause),
cause: cause,
}
}
func CtxE(ctx context.Context) BTE {
if ctx.Err() == nil {
return nil
}
return &bTE{
code: ContextError,
reason: fmt.Sprintf("%s (%s)", "context error", ctx.Err()),
cause: ctx.Err(),
}
}
func Chan(e BTE) chan BTE {
rv := make(chan BTE, 1)
rv <- e //buffered
return rv
}
//Context errors cascade quite a bit and tend to cause duplicate errors
//in the return channel. Try not to leak goroutiens by]
//blocking on them
func ChkContextError(ctx context.Context, rve chan BTE) bool {
if ctx.Err() != nil {
select {
case rve <- CtxE(ctx):
default:
}
return true
}
return false
}
func NoBlockError(e BTE, ch chan BTE) {
if e != nil {
select {
case ch <- e:
default:
}
}
}
// If you ask for next/prev point but there isn't one
const NoSuchPoint = 401
// Things like user timeout
const ContextError = 402
// Like tree depth
const InsertFailure = 403
const NoSuchStream = 404
// We don't have a write lock for this stream
const WrongEndpoint = 405
// The stream already exists
const StreamExists = 406
// Collection name is invalid
const InvalidCollection = 407
// Tag key is invalid
const InvalidTagKey = 408
// Tag value is invalid
const InvalidTagValue = 409
// Just in case
const AwesomenessExceedsThreshold = 410
// For commands accepting a limit argument, the passed limit is invalid
const InvalidLimit = 411
// If a set of tags is given to identify a single stream, but fails to do
// so
const AmbiguousTags = 412
// The start/end times are invalid
const InvalidTimeRange = 413
// The insertion is too big (that's what she said)
const InsertTooBig = 414
// Point widths are [0, 64)
const InvalidPointWidth = 415
// When an error has no code
const GenericError = 416
// When create() is called and the uuid and tags are the same
const SameStream = 417
// When create() is called and although the uuid is different, the tags are not unique
const AmbiguousStream = 418
// When a write op on an unmapped UUID is attempted
const ClusterDegraded = 419
// Just in case this is required after Prop 64
const BlazeIt = 420
// Generated in drivers when the arguments are the wrong type or length
const WrongArgs = 421
// Annotations cannot exceed 2MB
const AnnotationTooBig = 422
// The annotation version did not match
const AnnotationVersionMismatch = 423
// Fault injection is disabled (you need $BTRDB_ENABLE_FAULT_INJECTON=YES)
const FaultInjectionDisabled = 424
// Returned when you try insert NaN or Inf values
const BadValue = 425
// Used when the cluster is extremely overloaded and is shedding load to
// maintain stability
const ResourceDepleted = 426
// Returned when you try do changed ranges with bad versions
const InvalidVersions = 427
// Returned when the etcd cluster is unhealthy
const EtcdFailure = 428
// Returned if you try create a stream with a uuid that is the same as a
// previously deleted stream
const ReusedUUID = 429
// Returned if you try delete a stream while its attributes are being
// modified
const ConcurrentModification = 430
// Returned if you try to obliterate a stream on a node where
// BTRDB_ENABLE_OBLITERATE is not set to "YES"
const ObliterateDisabled = 431
// Generic error from ceph
const CephError = 432
// Means that a node ID is reused, as of BTrDB 4.8 a node id must be unique
const NodeExisted = 433
// Generic error returned when journalling
const JournalError = 434
// Used for assert statements
const InvariantFailure = 500
// Haha lol
const NotImplemented = 501