/
error.go
100 lines (84 loc) · 3.11 KB
/
error.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
// Copyright 2016 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.
package crdb
import "fmt"
// errorCause returns the original cause of the error, if possible. An
// error has a proximate cause if it's type is compatible with Go's
// errors.Unwrap() or pkg/errors' Cause(); the original cause is the
// end of the causal chain.
func errorCause(err error) error {
for err != nil {
if c, ok := err.(interface{ Cause() error }); ok {
err = c.Cause()
} else if c, ok := err.(interface{ Unwrap() error }); ok {
err = c.Unwrap()
} else {
break
}
}
return err
}
type txError struct {
cause error
}
// Error implements the error interface.
func (e *txError) Error() string { return e.cause.Error() }
// Cause implements the pkg/errors causer interface.
func (e *txError) Cause() error { return e.cause }
// Unwrap implements the go error causer interface.
func (e *txError) Unwrap() error { return e.cause }
// AmbiguousCommitError represents an error that left a transaction in an
// ambiguous state: unclear if it committed or not.
type AmbiguousCommitError struct {
txError
}
func newAmbiguousCommitError(err error) *AmbiguousCommitError {
return &AmbiguousCommitError{txError{cause: err}}
}
// MaxRetriesExceededError represents an error caused by retying the transaction
// too many times, without it ever succeeding.
type MaxRetriesExceededError struct {
txError
msg string
}
func newMaxRetriesExceededError(err error, maxRetries int) *MaxRetriesExceededError {
const msgPattern = "retrying txn failed after %d attempts. original error: %s."
return &MaxRetriesExceededError{
txError: txError{cause: err},
msg: fmt.Sprintf(msgPattern, maxRetries, err),
}
}
// Error implements the error interface.
func (e *MaxRetriesExceededError) Error() string { return e.msg }
// TxnRestartError represents an error when restarting a transaction. `cause` is
// the error from restarting the txn and `retryCause` is the original error which
// triggered the restart.
type TxnRestartError struct {
txError
retryCause error
msg string
}
func newTxnRestartError(err error, retryErr error) *TxnRestartError {
const msgPattern = "restarting txn failed. ROLLBACK TO SAVEPOINT " +
"encountered error: %s. Original error: %s."
return &TxnRestartError{
txError: txError{cause: err},
retryCause: retryErr,
msg: fmt.Sprintf(msgPattern, err, retryErr),
}
}
// Error implements the error interface.
func (e *TxnRestartError) Error() string { return e.msg }
// RetryCause returns the error that caused the transaction to be restarted.
func (e *TxnRestartError) RetryCause() error { return e.retryCause }