1 package database
2 3 import "fmt"
4 5 // ErrorCode identifies a kind of error.
6 type ErrorCode int
7 8 // These constants are used to identify a specific database DBError.
9 const (
10 // ErrDbTypeRegistered indicates two different database drivers attempt to
11 // register with the name database type.
12 ErrDbTypeRegistered ErrorCode = iota
13 // ErrDbUnknownType indicates there is no driver registered for the specified
14 // database type.
15 ErrDbUnknownType
16 // ErrDbDoesNotExist indicates open is called for a database that does not
17 // exist.
18 ErrDbDoesNotExist
19 // ErrDbExists indicates create is called for a database that already exists.
20 ErrDbExists
21 // ErrDbNotOpen indicates a database instance is accessed before it is opened
22 // or after it is closed.
23 ErrDbNotOpen
24 // ErrDbAlreadyOpen indicates open was called on a database that is already
25 // open.
26 ErrDbAlreadyOpen
27 // ErrInvalid indicates the specified database is not valid.
28 ErrInvalid
29 // ErrCorruption indicates a checksum failure occurred which invariably means
30 // the database is corrupt.
31 ErrCorruption
32 // ErrTxClosed indicates an attempt was made to commit or rollback a
33 // transaction that has already had one of those operations performed.
34 ErrTxClosed
35 // ErrTxNotWritable indicates an operation that requires write access to the
36 // database was attempted against a read-only transaction.
37 ErrTxNotWritable
38 // ErrBucketNotFound indicates an attempt to access a bucket that has not
39 // been created yet.
40 ErrBucketNotFound
41 // ErrBucketExists indicates an attempt to create a bucket that already
42 // exists.
43 ErrBucketExists
44 // ErrBucketNameRequired indicates an attempt to create a bucket with a blank
45 // name.
46 ErrBucketNameRequired
47 // ErrKeyRequired indicates at attempt to insert a zero-length key.
48 ErrKeyRequired
49 // ErrKeyTooLarge indicates an attmempt to insert a key that is larger than
50 // the max allowed key size. The max key size depends on the specific backend
51 // driver being used. As a general rule, key txsizes should be relatively, so
52 // this should rarely be an issue.
53 ErrKeyTooLarge
54 // ErrValueTooLarge indicates an attmpt to insert a value that is larger than
55 // max allowed value size. The max key size depends on the specific backend
56 // driver being used.
57 ErrValueTooLarge
58 // ErrIncompatibleValue indicates the value in question is invalid for the
59 // specific requested operation. For example, trying create or delete a
60 // bucket with an existing non-bucket key, attempting to create or delete a
61 // non-bucket key with an existing bucket key, or trying to delete a value
62 // via a cursor when it points to a nested bucket.
63 ErrIncompatibleValue
64 // ErrBlockNotFound indicates a block with the provided hash does not exist
65 // in the database.
66 ErrBlockNotFound
67 // ErrBlockExists indicates a block with the provided hash already exists in
68 // the database.
69 ErrBlockExists
70 // ErrBlockRegionInvalid indicates a region that exceeds the bounds of the
71 // specified block was requested. When the hash provided by the region does
72 // not correspond to an existing block, the error will be ErrBlockNotFound
73 // instead.
74 ErrBlockRegionInvalid
75 // ErrDriverSpecific indicates the Err field is a driver-specific error. This
76 // provides a mechanism for drivers to plug-in their own custom errors for
77 // any situations which aren't already covered by the error codes provided by
78 // this package.
79 ErrDriverSpecific
80 // numErrorCodes is the maximum error code number used in tests.
81 numErrorCodes
82 )
83 84 // Map of ErrorCode values back to their constant names for pretty printing.
85 var errorCodeStrings = map[ErrorCode]string{
86 ErrDbTypeRegistered: "ErrDbTypeRegistered",
87 ErrDbUnknownType: "ErrDbUnknownType",
88 ErrDbDoesNotExist: "ErrDbDoesNotExist",
89 ErrDbExists: "ErrDbExists",
90 ErrDbNotOpen: "ErrDbNotOpen",
91 ErrDbAlreadyOpen: "ErrDbAlreadyOpen",
92 ErrInvalid: "ErrInvalid",
93 ErrCorruption: "ErrCorruption",
94 ErrTxClosed: "ErrTxClosed",
95 ErrTxNotWritable: "ErrTxNotWritable",
96 ErrBucketNotFound: "ErrBucketNotFound",
97 ErrBucketExists: "ErrBucketExists",
98 ErrBucketNameRequired: "ErrBucketNameRequired",
99 ErrKeyRequired: "ErrKeyRequired",
100 ErrKeyTooLarge: "ErrKeyTooLarge",
101 ErrValueTooLarge: "ErrValueTooLarge",
102 ErrIncompatibleValue: "ErrIncompatibleValue",
103 ErrBlockNotFound: "ErrBlockNotFound",
104 ErrBlockExists: "ErrBlockExists",
105 ErrBlockRegionInvalid: "ErrBlockRegionInvalid",
106 ErrDriverSpecific: "ErrDriverSpecific",
107 }
108 109 // String returns the ErrorCode as a human-readable name.
110 func (e ErrorCode) String() string {
111 if s := errorCodeStrings[e]; s != "" {
112 return s
113 }
114 return fmt.Sprintf("Unknown ErrorCode (%d)", int(e))
115 }
116 117 // DBError provides a single type for errors that can happen during database
118 // operation. It is used to indicate several types of failures including errors
119 // with caller requests such as specifying invalid block regions or attempting
120 // to access data against closed database transactions, driver errors, errors
121 // retrieving data, and errors communicating with database servers.
122 //
123 // The caller can use type assertions to determine if an error is an DBError and
124 // access the ErrorCode field to ascertain the specific reason for the failure.
125 //
126 // The ErrDriverSpecific error code will also have the Err field set with the
127 // underlying error. Depending on the backend driver, the Err field might be set
128 // to the underlying error for other error codes as well.
129 type DBError struct {
130 ErrorCode ErrorCode // Describes the kind of error
131 Description string // Human readable description of the issue
132 Err error // Underlying error
133 }
134 135 // DBError satisfies the error interface and prints human-readable errors.
136 func (e DBError) Error() string {
137 if e.Err != nil {
138 return e.Description + ": " + e.Err.Error()
139 }
140 return e.Description
141 }
142 143 // makeError creates an DBError given a set of arguments. The error code must be
144 // one of the error codes provided by this package.
145 func makeError(c ErrorCode, desc string, e error) DBError {
146 return DBError{ErrorCode: c, Description: desc, Err: e}
147 }
148