forked from btcsuite/btcwallet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.go
91 lines (76 loc) · 2.96 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
// Copyright (c) 2015 The btcsuite developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package wtxmgr
import "fmt"
// ErrorCode identifies a category of error.
type ErrorCode uint8
// These constants are used to identify a specific Error.
const (
// ErrDatabase indicates an error with the underlying database. When
// this error code is set, the Err field of the Error will be
// set to the underlying error returned from the database.
ErrDatabase ErrorCode = iota
// ErrData describes an error where data stored in the transaction
// database is incorrect. This may be due to missing values, values of
// wrong sizes, or data from different buckets that is inconsistent with
// itself. Recovering from an ErrData requires rebuilding all
// transaction history or manual database surgery. If the failure was
// not due to data corruption, this error category indicates a
// programming error in this package.
ErrData
// ErrInput describes an error where the variables passed into this
// function by the caller are obviously incorrect. Examples include
// passing transactions which do not serialize, or attempting to insert
// a credit at an index for which no transaction output exists.
ErrInput
// ErrAlreadyExists describes an error where creating the store cannot
// continue because a store already exists in the namespace.
ErrAlreadyExists
// ErrNoExists describes an error where the store cannot be opened due to
// it not already existing in the namespace. This error should be
// handled by creating a new store.
ErrNoExists
// ErrUnknownVersion describes an error where the store already exists
// but the database version is newer than latest version known to this
// software. This likely indicates an outdated binary.
ErrUnknownVersion
)
var errStrs = [...]string{
ErrDatabase: "ErrDatabase",
ErrData: "ErrData",
ErrInput: "ErrInput",
ErrAlreadyExists: "ErrAlreadyExists",
ErrNoExists: "ErrNoExists",
ErrUnknownVersion: "ErrUnknownVersion",
}
// String returns the ErrorCode as a human-readable name.
func (e ErrorCode) String() string {
if e < ErrorCode(len(errStrs)) {
return errStrs[e]
}
return fmt.Sprintf("ErrorCode(%d)", e)
}
// Error provides a single type for errors that can happen during Store
// operation.
type Error struct {
Code ErrorCode // Describes the kind of error
Desc string // Human readable description of the issue
Err error // Underlying error, optional
}
// Error satisfies the error interface and prints human-readable errors.
func (e Error) Error() string {
if e.Err != nil {
return e.Desc + ": " + e.Err.Error()
}
return e.Desc
}
func storeError(c ErrorCode, desc string, err error) Error {
return Error{Code: c, Desc: desc, Err: err}
}
// IsNoExists returns whether an error is a Error with the ErrNoExists error
// code.
func IsNoExists(err error) bool {
serr, ok := err.(Error)
return ok && serr.Code == ErrNoExists
}