forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.go
591 lines (482 loc) · 16.8 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
// Copyright 2014 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// Author: Spencer Kimball (spencer.kimball@gmail.com)
package roachpb
import (
"errors"
"fmt"
"github.com/cockroachdb/cockroach/util/caller"
"github.com/cockroachdb/cockroach/util/retry"
)
// ResponseWithError is a tuple of a BatchResponse and an error. It is used to
// pass around a BatchResponse with its associated error where that
// entanglement is necessary (e.g. channels, methods that need to return
// another error in addition to this one).
type ResponseWithError struct {
Reply *BatchResponse
Err *Error
}
// ErrorUnexpectedlySet creates a string to panic with when a response (typically
// a roachpb.BatchResponse) unexpectedly has Error set in its response header.
func ErrorUnexpectedlySet(culprit, response interface{}) string {
return fmt.Sprintf("error is unexpectedly set, culprit is %T:\n%+v", culprit, response)
}
// transactionRestartError is an interface implemented by errors that cause
// a transaction to be restarted.
type transactionRestartError interface {
canRestartTransaction() TransactionRestart
}
// GetDetail returns an error detail associated with the error.
func (e *Error) GetDetail() ErrorDetailInterface {
if e == nil {
return nil
}
if e.Detail == nil {
// Unknown error detail; return the generic error.
return (*internalError)(e)
}
if err, ok := e.Detail.GetValue().(ErrorDetailInterface); ok {
return err
}
// Unknown error detail; return the generic error.
return (*internalError)(e)
}
// NewError creates an Error from the given error.
func NewError(err error) *Error {
if err == nil {
return nil
}
e := &Error{}
if intErr, ok := err.(*internalError); ok {
*e = *(*Error)(intErr)
} else {
e.setGoError(err)
}
return e
}
// NewErrorWithTxn creates an Error from the given error and a transaction.
func NewErrorWithTxn(err error, txn *Transaction) *Error {
e := NewError(err)
e.SetTxn(txn)
return e
}
// NewUErrorf creates an Error from the given error message. Used
// for user-facing errors.
func NewUErrorf(format string, a ...interface{}) *Error {
return NewError(fmt.Errorf(format, a...))
}
// NewErrorf creates an Error from the given error message. It is a
// passthrough to fmt.Errorf, with an additional prefix containing the
// filename and line number.
func NewErrorf(format string, a ...interface{}) *Error {
// Cannot use util.Errorf here due to cyclic dependency.
file, line, _ := caller.Lookup(1)
s := fmt.Sprintf("%s:%d: ", file, line)
return NewError(fmt.Errorf(s+format, a...))
}
// String implements fmt.Stringer.
func (e *Error) String() string {
return e.Message
}
type internalError Error
// Error implements error.
func (e *internalError) Error() string {
return (*Error)(e).String()
}
// message returns an error message.
func (e *internalError) message(_ *Error) string {
return (*Error)(e).String()
}
// CanRetry implements the retry.Retryable interface.
func (e *internalError) CanRetry() bool {
return e.Retryable
}
// canRestartTransaction implements the transactionRestartError interface.
func (e *internalError) canRestartTransaction() TransactionRestart {
return e.TransactionRestart
}
var _ ErrorDetailInterface = &internalError{}
// ErrorDetailInterface is an interface for each error detail.
type ErrorDetailInterface interface {
// message returns an error message.
message(*Error) string
}
// CanRetry implements the retry.Retryable interface.
func (e *Error) CanRetry() bool {
return e.Retryable
}
// GoError returns a Go error converted from Error.
func (e *Error) GoError() error {
if e == nil {
return nil
}
return errors.New(e.Message)
}
// setGoError sets Error using err.
func (e *Error) setGoError(err error) {
if e.Message != "" {
panic("cannot re-use roachpb.Error")
}
if sErr, ok := err.(ErrorDetailInterface); ok {
e.Message = sErr.message(e)
} else {
e.Message = err.Error()
}
if r, ok := err.(retry.Retryable); ok {
e.Retryable = r.CanRetry()
}
var isTxnError bool
if r, ok := err.(transactionRestartError); ok {
isTxnError = true
e.TransactionRestart = r.canRestartTransaction()
}
// If the specific error type exists in the detail union, set it.
detail := &ErrorDetail{}
if detail.SetValue(err) {
e.Detail = detail
} else if _, isInternalError := err.(*internalError); !isInternalError && isTxnError {
panic(fmt.Sprintf("transactionRestartError %T must be an ErrorDetail", err))
}
}
// SetTxn sets the txn and resets the error message.
// TODO(kaneda): Unexpose this method and make callers use NewErrorWithTxn.
func (e *Error) SetTxn(txn *Transaction) {
e.UnexposedTxn = txn
if txn != nil {
txnClone := txn.Clone()
e.UnexposedTxn = &txnClone
}
if e.Detail != nil {
if sErr, ok := e.Detail.GetValue().(ErrorDetailInterface); ok {
// Refresh the message as the txn is updated.
e.Message = sErr.message(e)
}
}
}
// GetTxn returns the txn.
func (e *Error) GetTxn() *Transaction {
if e == nil {
return nil
}
return e.UnexposedTxn
}
// SetErrorIndex sets the index of the error.
func (e *Error) SetErrorIndex(index int32) {
e.Index = &ErrPosition{Index: index}
}
// StripErrorTransaction strips the txn information in the error.
func (e *Error) StripErrorTransaction() {
// Do not call SetTxn() as we do not want to update e.Message with nil txn.
e.UnexposedTxn = nil
}
// Error formats error.
func (e *NodeUnavailableError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (*NodeUnavailableError) message(_ *Error) string {
return "node unavailable; try another peer"
}
var _ ErrorDetailInterface = &NodeUnavailableError{}
// Error formats error.
func (e *NotLeaderError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *NotLeaderError) message(_ *Error) string {
return fmt.Sprintf("range %d: replica %s not leader; leader is %s", e.RangeID, e.Replica, e.Leader)
}
var _ ErrorDetailInterface = &NotLeaderError{}
// Error formats error.
func (e *LeaseRejectedError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *LeaseRejectedError) message(_ *Error) string {
return fmt.Sprintf("cannot replace lease %s with %s: %s", e.Existing, e.Requested, e.Message)
}
// CanRetry indicates that this error can not be retried; it should never
// make it back to the client anyways.
func (*LeaseRejectedError) CanRetry() bool {
return false
}
var _ ErrorDetailInterface = &LeaseRejectedError{}
// NewSendError creates a SendError. canRetry should be true in most cases; the
// only non-retryable SendErrors are for things like malformed (and not merely
// unresolvable) addresses.
func NewSendError(msg string, canRetry bool) *SendError {
return &SendError{Message: msg, Retryable: canRetry}
}
// Error formats error.
func (s SendError) Error() string {
return s.message(nil)
}
// message returns an error message.
func (s *SendError) message(_ *Error) string {
return "failed to send RPC: " + s.Message
}
// CanRetry implements the Retryable interface.
func (s SendError) CanRetry() bool { return s.Retryable }
var _ ErrorDetailInterface = &SendError{}
// NewRangeNotFoundError initializes a new RangeNotFoundError.
func NewRangeNotFoundError(rangeID RangeID) *RangeNotFoundError {
return &RangeNotFoundError{
RangeID: rangeID,
}
}
// Error formats error.
func (e *RangeNotFoundError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *RangeNotFoundError) message(_ *Error) string {
return fmt.Sprintf("range %d was not found", e.RangeID)
}
// CanRetry indicates whether or not this RangeNotFoundError can be retried.
func (*RangeNotFoundError) CanRetry() bool {
return true
}
var _ ErrorDetailInterface = &RangeNotFoundError{}
// NewRangeKeyMismatchError initializes a new RangeKeyMismatchError.
func NewRangeKeyMismatchError(start, end Key, desc *RangeDescriptor) *RangeKeyMismatchError {
return &RangeKeyMismatchError{
RequestStartKey: start,
RequestEndKey: end,
Range: desc,
}
}
// Error formats error.
func (e *RangeKeyMismatchError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *RangeKeyMismatchError) message(_ *Error) string {
if e.Range != nil {
return fmt.Sprintf("key range %s-%s outside of bounds of range %s-%s",
e.RequestStartKey, e.RequestEndKey, e.Range.StartKey, e.Range.EndKey)
}
return fmt.Sprintf("key range %s-%s could not be located within a range on store", e.RequestStartKey, e.RequestEndKey)
}
// CanRetry indicates whether or not this RangeKeyMismatchError can be retried.
func (*RangeKeyMismatchError) CanRetry() bool {
return true
}
var _ ErrorDetailInterface = &RangeNotFoundError{}
// Error formats error.
func (e *TransactionAbortedError) Error() string {
return fmt.Sprintf("txn aborted")
}
// message returns an error message.
func (e *TransactionAbortedError) message(pErr *Error) string {
return fmt.Sprintf("txn aborted %s", pErr.GetTxn())
}
var _ ErrorDetailInterface = &TransactionAbortedError{}
var _ transactionRestartError = &TransactionAbortedError{}
// canRestartTransaction implements the transactionRestartError interface.
func (*TransactionAbortedError) canRestartTransaction() TransactionRestart {
return TransactionRestart_BACKOFF
}
// NewTransactionAbortedError initializes a new TransactionAbortedError.
func NewTransactionAbortedError() *TransactionAbortedError {
return &TransactionAbortedError{}
}
// NewTransactionPushError initializes a new TransactionPushError.
// The argument is copied.
func NewTransactionPushError(pusheeTxn Transaction) *TransactionPushError {
// Note: this error will cause a txn restart. The error that the client
// receives contains a txn that might have a modified priority.
return &TransactionPushError{PusheeTxn: pusheeTxn.Clone()}
}
// Error formats error.
func (e *TransactionPushError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *TransactionPushError) message(pErr *Error) string {
if pErr.GetTxn() == nil {
return fmt.Sprintf("failed to push %s", e.PusheeTxn)
}
return fmt.Sprintf("txn %s failed to push %s", pErr.GetTxn(), e.PusheeTxn)
}
var _ ErrorDetailInterface = &TransactionPushError{}
var _ transactionRestartError = &TransactionPushError{}
// canRestartTransaction implements the transactionRestartError interface.
func (*TransactionPushError) canRestartTransaction() TransactionRestart {
return TransactionRestart_BACKOFF
}
// NewTransactionRetryError initializes a new TransactionRetryError.
func NewTransactionRetryError() *TransactionRetryError {
return &TransactionRetryError{}
}
// Error formats error.
// TODO(kaneda): Delete this method once we fully unimplement error for every
// error detail.
func (e *TransactionRetryError) Error() string {
return fmt.Sprintf("retry txn")
}
// message returns an error message.
func (e *TransactionRetryError) message(pErr *Error) string {
return fmt.Sprintf("retry txn %s", pErr.GetTxn())
}
var _ ErrorDetailInterface = &TransactionRetryError{}
var _ transactionRestartError = &TransactionRetryError{}
// canRestartTransaction implements the transactionRestartError interface.
func (*TransactionRetryError) canRestartTransaction() TransactionRestart {
return TransactionRestart_IMMEDIATE
}
// NewTransactionStatusError initializes a new TransactionStatusError from
// the given message.
func NewTransactionStatusError(msg string) *TransactionStatusError {
return &TransactionStatusError{Msg: msg}
}
// Error formats error.
func (e *TransactionStatusError) Error() string {
return e.Msg
}
// message returns an error message.
func (e *TransactionStatusError) message(pErr *Error) string {
return fmt.Sprintf("txn %s: %s", pErr.GetTxn(), e.Msg)
}
var _ ErrorDetailInterface = &TransactionStatusError{}
// Error formats error.
func (e *WriteIntentError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *WriteIntentError) message(_ *Error) string {
var keys []Key
for _, intent := range e.Intents {
keys = append(keys, intent.Key)
}
return fmt.Sprintf("conflicting intents on %v: resolved? %t", keys, e.Resolved)
}
var _ ErrorDetailInterface = &WriteIntentError{}
// Error formats error.
func (e *WriteTooOldError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *WriteTooOldError) message(_ *Error) string {
return fmt.Sprintf("write too old: timestamp %s <= %s", e.Timestamp, e.ExistingTimestamp)
}
var _ ErrorDetailInterface = &WriteTooOldError{}
// NewReadWithinUncertaintyIntervalError creates a new uncertainty retry error.
// The read and existing timestamps are purely informational and used for
// formatting the error message.
func NewReadWithinUncertaintyIntervalError(readTS, existingTS Timestamp) *ReadWithinUncertaintyIntervalError {
return &ReadWithinUncertaintyIntervalError{
ReadTimestamp: readTS,
ExistingTimestamp: existingTS,
}
}
// Error formats error.
func (e *ReadWithinUncertaintyIntervalError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *ReadWithinUncertaintyIntervalError) message(_ *Error) string {
return fmt.Sprintf("read at time %s encountered previous write with future timestamp %s within uncertainty interval", e.ReadTimestamp, e.ExistingTimestamp)
}
var _ ErrorDetailInterface = &ReadWithinUncertaintyIntervalError{}
var _ transactionRestartError = &ReadWithinUncertaintyIntervalError{}
// canRestartTransaction implements the transactionRestartError interface.
func (*ReadWithinUncertaintyIntervalError) canRestartTransaction() TransactionRestart {
return TransactionRestart_IMMEDIATE
}
// Error formats error.
func (e *OpRequiresTxnError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *OpRequiresTxnError) message(_ *Error) string {
return "the operation requires transactional context"
}
var _ ErrorDetailInterface = &OpRequiresTxnError{}
// Error formats error.
func (e *ConditionFailedError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *ConditionFailedError) message(_ *Error) string {
return fmt.Sprintf("unexpected value: %s", e.ActualValue)
}
var _ ErrorDetailInterface = &ConditionFailedError{}
// Error formats error.
func (e *RaftGroupDeletedError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (*RaftGroupDeletedError) message(_ *Error) string {
return "raft group deleted"
}
var _ ErrorDetailInterface = &RaftGroupDeletedError{}
// Error formats error.
func (e *ReplicaCorruptionError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *ReplicaCorruptionError) message(_ *Error) string {
return fmt.Sprintf("replica corruption (processed=%t): %s", e.Processed, e.ErrorMsg)
}
var _ ErrorDetailInterface = &ReplicaCorruptionError{}
// Error formats error.
func (e *LeaseVersionChangedError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (*LeaseVersionChangedError) message(_ *Error) string {
return "lease version changed"
}
var _ ErrorDetailInterface = &LeaseVersionChangedError{}
// Error formats error.
func (e *DidntUpdateDescriptorError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (*DidntUpdateDescriptorError) message(_ *Error) string {
return "didn't update the table descriptor"
}
var _ ErrorDetailInterface = &DidntUpdateDescriptorError{}
// Error formats error.
func (e *SqlTransactionCommittedError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (*SqlTransactionCommittedError) message(_ *Error) string {
return "current transaction is committed, commands ignored until end of transaction block"
}
var _ ErrorDetailInterface = &SqlTransactionCommittedError{}
// Error formats error.
func (e *SqlTransactionAbortedError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (e *SqlTransactionAbortedError) message(_ *Error) string {
msg := "current transaction is aborted, commands ignored until end of transaction block"
if e.CustomMsg != "" {
msg += "; " + e.CustomMsg
}
return msg
}
var _ ErrorDetailInterface = &SqlTransactionAbortedError{}
// Error formats error.
func (e *ExistingSchemaChangeLeaseError) Error() string {
return e.message(nil)
}
// message returns an error message.
func (*ExistingSchemaChangeLeaseError) message(_ *Error) string {
return "an outstanding schema change lease exists"
}
var _ ErrorDetailInterface = &ExistingSchemaChangeLeaseError{}