bbolt_rs/common/
errors.rs

1use std::io;
2use thiserror::Error;
3
4/// Bolt database errors
5#[derive(Error, Debug)]
6pub enum Error {
7  // These errors can be returned when opening or calling methods on a DB.
8  /// DatabaseNotOpen is returned when a DB instance is accessed before it
9  /// is opened or after it is closed.
10  #[error("database not open")]
11  DatabaseNotOpen,
12  /// DatabaseOpen is returned when opening a database that is
13  /// already open.
14  #[error("database already open")]
15  DatabaseOpen,
16  /// InvalidDatabase is returned when both meta pages on a database are invalid.
17  /// This typically occurs when a file is not a bolt database.
18  #[error("invalid database - meta_can_read: `{0}`")]
19  InvalidDatabase(bool),
20  /// InvalidMapping is returned when the database file fails to get mapped.
21  #[error("database isn't correctly mapped")]
22  InvalidMapping,
23  /// ErrVersionMismatch is returned when the data file was created with a
24  /// different version of Bolt.
25  #[error("version mismatch")]
26  VersionMismatch,
27  /// Checksum is returned when either meta page checksum does not match.
28  #[error("checksum mismatch")]
29  ChecksumMismatch,
30  /// File size is below the minimum size a Bolt database could be
31  #[error("file size too small: `{0}`")]
32  FileSizeTooSmall(u64),
33  /// Timeout is returned when a database cannot obtain an exclusive lock
34  /// on the data file after the timeout passed to Open().
35  #[error("timeout")]
36  Timeout,
37  /// FreePagesNotLoaded is returned when a readonly transaction without
38  /// preloading the free pages is trying to access the free pages.
39  #[error("free pages are not pre-loaded")]
40  FreePagesNotLoaded,
41  // These errors can occur when putting or deleting a value or a bucket.
42  /// BucketNotFound is returned when trying to access a bucket that has
43  /// not been created yet.
44  #[error("bucket not found")]
45  BucketNotFound,
46  /// BucketExists is returned when creating a bucket that already exists.
47  #[error("bucket already exists")]
48  BucketExists,
49  /// BucketNameRequired is returned when creating a bucket with a blank name.
50  #[error("bucket name required")]
51  BucketNameRequired,
52  /// KeyRequired is returned when inserting a zero-length key.
53  #[error("key required")]
54  KeyRequired,
55  /// KeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
56  #[error("key too large")]
57  KeyTooLarge,
58  /// ValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
59  #[error("value too large")]
60  ValueTooLarge,
61  /// IncompatibleValue is returned when trying to create or delete a bucket
62  /// on an existing non-bucket key or when trying to create or delete a
63  /// non-bucket key on an existing bucket key.
64  #[error("incompatible value")]
65  IncompatibleValue,
66  /// File size is below the minimum size a Bolt database could be
67  #[error("mmap too small: `{0}`")]
68  MMapTooSmall(u64),
69  /// File size is larger than the maximum size a Bolt database could be
70  #[error("mmap too large")]
71  MMapTooLarge,
72  /// Batch call failed. Try again without batch
73  #[error("try function without batch call")]
74  TrySolo,
75  /// Batch is disabled
76  #[error("max batch delay or length is set to 0")]
77  BatchDisabled,
78  /// Chained errors from other sources
79  #[error(transparent)]
80  IO(#[from] io::Error),
81  /// User defined error
82  #[error(transparent)]
83  Other(#[from] anyhow::Error),
84}
85
86impl PartialEq for Error {
87  fn eq(&self, other: &Self) -> bool {
88    matches!(
89      (self, other),
90      (&Error::DatabaseNotOpen, &Error::DatabaseNotOpen)
91        | (Error::DatabaseOpen, Error::DatabaseOpen)
92        | (Error::InvalidDatabase(_), Error::InvalidDatabase(_))
93        | (Error::InvalidMapping, Error::InvalidMapping)
94        | (Error::VersionMismatch, Error::VersionMismatch)
95        | (Error::ChecksumMismatch, Error::ChecksumMismatch)
96        | (Error::FileSizeTooSmall(_), Error::FileSizeTooSmall(_))
97        | (Error::Timeout, Error::Timeout)
98        | (Error::FreePagesNotLoaded, Error::FreePagesNotLoaded)
99        | (Error::BucketNotFound, Error::BucketNotFound)
100        | (Error::BucketExists, Error::BucketExists)
101        | (Error::BucketNameRequired, Error::BucketNameRequired)
102        | (Error::KeyRequired, Error::KeyRequired)
103        | (Error::KeyTooLarge, Error::KeyTooLarge)
104        | (Error::ValueTooLarge, Error::ValueTooLarge)
105        | (Error::IncompatibleValue, Error::IncompatibleValue)
106        | (Error::MMapTooSmall(_), Error::MMapTooSmall(_))
107        | (Error::MMapTooLarge, Error::MMapTooLarge)
108        | (Error::TrySolo, Error::TrySolo)
109        | (Error::BatchDisabled, Error::BatchDisabled)
110    )
111  }
112}
113
114impl Eq for Error {}
115
116pub type Result<T, E = Error> = std::result::Result<T, E>;