1 package wtxmgr
2 3 import "fmt"
4 5 // ErrorCode identifies a category of error.
6 type ErrorCode uint8
7 8 // These constants are used to identify a specific TxMgrError.
9 const (
10 // ErrDatabase indicates an error with the underlying database. When this error code is set, the Err field of the
11 // TxMgrError will be set to the underlying error returned from the database.
12 ErrDatabase ErrorCode = iota
13 // ErrData describes an error where data stored in the transaction database is incorrect. This may be due to missing
14 // values, values of wrong txsizes, or data from different buckets that is inconsistent with itself. Recovering from
15 // an ErrData requires rebuilding all transaction history or manual database surgery. If the failure was not due to
16 // data corruption, this error category indicates a programming error in this package.
17 ErrData
18 // ErrInput describes an error where the variables passed into this function by the caller are obviously incorrect.
19 // Examples include passing transactions which do not serialize, or attempting to insert a credit at an index for
20 // which no transaction output exists.
21 ErrInput
22 // ErrAlreadyExists describes an error where creating the store cannot continue because a store already exists in
23 // the namespace.
24 ErrAlreadyExists
25 // ErrNoExists describes an error where the store cannot be opened due to it not already existing in the namespace.
26 // This error should be handled by creating a new store.
27 ErrNoExists
28 // ErrNeedsUpgrade describes an error during store opening where the database contains an older version of the
29 // store.
30 ErrNeedsUpgrade
31 // ErrUnknownVersion describes an error where the store already exists but the database version is newer than latest
32 // version known to this software. This likely indicates an outdated binary.
33 ErrUnknownVersion
34 )
35 36 var errStrs = [...]string{
37 ErrDatabase: "ErrDatabase",
38 ErrData: "ErrData",
39 ErrInput: "ErrInput",
40 ErrAlreadyExists: "ErrAlreadyExists",
41 ErrNoExists: "ErrNoExists",
42 ErrNeedsUpgrade: "ErrNeedsUpgrade",
43 ErrUnknownVersion: "ErrUnknownVersion",
44 }
45 46 // String returns the ErrorCode as a human-readable name.
47 func (e ErrorCode) String() string {
48 if e < ErrorCode(len(errStrs)) {
49 return errStrs[e]
50 }
51 return fmt.Sprintf("ErrorCode(%d)", e)
52 }
53 54 // TxMgrError provides a single type for errors that can happen during Store operation.
55 type TxMgrError struct {
56 Code ErrorCode // Describes the kind of error
57 Desc string // Human readable description of the issue
58 Err error // Underlying error, optional
59 }
60 61 // TxMgrError satisfies the error interface and prints human-readable errors.
62 func (e TxMgrError) Error() string {
63 if e.Err != nil {
64 return e.Desc + ": " + e.Err.Error()
65 }
66 return e.Desc
67 }
68 func storeError(c ErrorCode, desc string, e error) TxMgrError {
69 return TxMgrError{Code: c, Desc: desc, Err: e}
70 }
71 72 // IsNoExists returns whether an error is a TxMgrError with the ErrNoExists error code.
73 func IsNoExists(e error) bool {
74 serr, ok := e.(TxMgrError)
75 return ok && serr.Code == ErrNoExists
76 }
77