1 // Copyright (c) 2015-2016 The btcsuite developers
2 // Use of this source code is governed by an ISC
3 // license that can be found in the LICENSE file.
9 // ErrorCode identifies a kind of error.
12 // These constants are used to identify a specific database Error.
14 // **************************************
15 // Errors related to driver registration.
16 // **************************************
18 // ErrDbTypeRegistered indicates two different database drivers
19 // attempt to register with the name database type.
20 ErrDbTypeRegistered ErrorCode = iota
22 // *************************************
23 // Errors related to database functions.
24 // *************************************
26 // ErrDbUnknownType indicates there is no driver registered for
27 // the specified database type.
30 // ErrDbDoesNotExist indicates open is called for a database that
34 // ErrDbExists indicates create is called for a database that
38 // ErrDbNotOpen indicates a database instance is accessed before
39 // it is opened or after it is closed.
42 // ErrDbAlreadyOpen indicates open was called on a database that
46 // ErrInvalid indicates the specified database is not valid.
49 // ErrCorruption indicates a checksum failure occurred which invariably
50 // means the database is corrupt.
53 // ****************************************
54 // Errors related to database transactions.
55 // ****************************************
57 // ErrTxClosed indicates an attempt was made to commit or rollback a
58 // transaction that has already had one of those operations performed.
61 // ErrTxNotWritable indicates an operation that requires write access to
62 // the database was attempted against a read-only transaction.
65 // **************************************
66 // Errors related to metadata operations.
67 // **************************************
69 // ErrBucketNotFound indicates an attempt to access a bucket that has
70 // not been created yet.
73 // ErrBucketExists indicates an attempt to create a bucket that already
77 // ErrBucketNameRequired indicates an attempt to create a bucket with a
81 // ErrKeyRequired indicates at attempt to insert a zero-length key.
84 // ErrKeyTooLarge indicates an attmempt to insert a key that is larger
85 // than the max allowed key size. The max key size depends on the
86 // specific backend driver being used. As a general rule, key sizes
87 // should be relatively, so this should rarely be an issue.
90 // ErrValueTooLarge indicates an attmpt to insert a value that is larger
91 // than max allowed value size. The max key size depends on the
92 // specific backend driver being used.
95 // ErrIncompatibleValue indicates the value in question is invalid for
96 // the specific requested operation. For example, trying create or
97 // delete a bucket with an existing non-bucket key, attempting to create
98 // or delete a non-bucket key with an existing bucket key, or trying to
99 // delete a value via a cursor when it points to a nested bucket.
102 // ***************************************
103 // Errors related to block I/O operations.
104 // ***************************************
106 // ErrBlockNotFound indicates a block with the provided hash does not
107 // exist in the database.
110 // ErrBlockExists indicates a block with the provided hash already
111 // exists in the database.
114 // ErrBlockRegionInvalid indicates a region that exceeds the bounds of
115 // the specified block was requested. When the hash provided by the
116 // region does not correspond to an existing block, the error will be
117 // ErrBlockNotFound instead.
118 ErrBlockRegionInvalid
120 // ***********************************
121 // Support for driver-specific errors.
122 // ***********************************
124 // ErrDriverSpecific indicates the Err field is a driver-specific error.
125 // This provides a mechanism for drivers to plug-in their own custom
126 // errors for any situations which aren't already covered by the error
127 // codes provided by this package.
130 // numErrorCodes is the maximum error code number used in tests.
134 // Map of ErrorCode values back to their constant names for pretty printing.
135 var errorCodeStrings = map[ErrorCode]string{
136 ErrDbTypeRegistered: "ErrDbTypeRegistered",
137 ErrDbUnknownType: "ErrDbUnknownType",
138 ErrDbDoesNotExist: "ErrDbDoesNotExist",
139 ErrDbExists: "ErrDbExists",
140 ErrDbNotOpen: "ErrDbNotOpen",
141 ErrDbAlreadyOpen: "ErrDbAlreadyOpen",
142 ErrInvalid: "ErrInvalid",
143 ErrCorruption: "ErrCorruption",
144 ErrTxClosed: "ErrTxClosed",
145 ErrTxNotWritable: "ErrTxNotWritable",
146 ErrBucketNotFound: "ErrBucketNotFound",
147 ErrBucketExists: "ErrBucketExists",
148 ErrBucketNameRequired: "ErrBucketNameRequired",
149 ErrKeyRequired: "ErrKeyRequired",
150 ErrKeyTooLarge: "ErrKeyTooLarge",
151 ErrValueTooLarge: "ErrValueTooLarge",
152 ErrIncompatibleValue: "ErrIncompatibleValue",
153 ErrBlockNotFound: "ErrBlockNotFound",
154 ErrBlockExists: "ErrBlockExists",
155 ErrBlockRegionInvalid: "ErrBlockRegionInvalid",
156 ErrDriverSpecific: "ErrDriverSpecific",
159 // String returns the ErrorCode as a human-readable name.
160 func (e ErrorCode) String() string {
161 if s := errorCodeStrings[e]; s != "" {
164 return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
167 // Error provides a single type for errors that can happen during database
168 // operation. It is used to indicate several types of failures including errors
169 // with caller requests such as specifying invalid block regions or attempting
170 // to access data against closed database transactions, driver errors, errors
171 // retrieving data, and errors communicating with database servers.
173 // The caller can use type assertions to determine if an error is an Error and
174 // access the ErrorCode field to ascertain the specific reason for the failure.
176 // The ErrDriverSpecific error code will also have the Err field set with the
177 // underlying error. Depending on the backend driver, the Err field might be
178 // set to the underlying error for other error codes as well.
180 ErrorCode ErrorCode // Describes the kind of error
181 Description string // Human readable description of the issue
182 Err error // Underlying error
185 // Error satisfies the error interface and prints human-readable errors.
186 func (e Error) Error() string {
188 return e.Description + ": " + e.Err.Error()
193 // makeError creates an Error given a set of arguments. The error code must
194 // be one of the error codes provided by this package.
195 func makeError(c ErrorCode, desc string, err error) Error {
196 return Error{ErrorCode: c, Description: desc, Err: err}