github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/database/error.go (about)

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