aingle_rkv/
error.rs

1// Copyright 2018-2019 Mozilla
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not use
4// this file except in compliance with the License. You may obtain a copy of the
5// License at http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software distributed
7// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
8// CONDITIONS OF ANY KIND, either express or implied. See the License for the
9// specific language governing permissions and limitations under the License.
10
11use std::path::PathBuf;
12
13use bincode;
14use thiserror::Error;
15use aingle_lmdb as lmdb;
16
17use crate::value::Type;
18
19#[derive(Debug, Error)]
20pub enum DataError {
21    #[error("unknown type tag: {0}")]
22    UnknownType(u8),
23
24    #[error("unexpected type tag: expected {expected}, got {actual}")]
25    UnexpectedType {
26        expected: Type,
27        actual: Type,
28    },
29
30    #[error("empty data; expected tag")]
31    Empty,
32
33    #[error("invalid value for type {value_type}: {err}")]
34    DecodingError {
35        value_type: Type,
36        err: Box<bincode::ErrorKind>,
37    },
38
39    #[error("couldn't encode value: {0}")]
40    EncodingError(Box<bincode::ErrorKind>),
41
42    #[error("invalid uuid bytes")]
43    InvalidUuid,
44}
45
46impl From<Box<bincode::ErrorKind>> for DataError {
47    fn from(e: Box<bincode::ErrorKind>) -> DataError {
48        DataError::EncodingError(e)
49    }
50}
51
52#[derive(Debug, Error)]
53pub enum StoreError {
54    #[error("I/O error: {0:?}")]
55    IoError(::std::io::Error),
56
57    #[error("directory does not exist or not a directory: {0:?}")]
58    DirectoryDoesNotExistError(PathBuf),
59
60    #[error("data error: {0:?}")]
61    DataError(DataError),
62
63    #[error("lmdb error: {0}")]
64    LmdbError(aingle_lmdb::Error),
65
66    #[error("read transaction already exists in thread {0:?}")]
67    ReadTransactionAlreadyExists(::std::thread::ThreadId),
68
69    #[error("attempted to open DB during transaction in thread {0:?}")]
70    OpenAttemptedDuringTransaction(::std::thread::ThreadId),
71}
72
73impl StoreError {
74    pub fn open_during_transaction() -> StoreError {
75        StoreError::OpenAttemptedDuringTransaction(::std::thread::current().id())
76    }
77}
78
79impl From<aingle_lmdb::Error> for StoreError {
80    fn from(e: aingle_lmdb::Error) -> StoreError {
81        match e {
82            aingle_lmdb::Error::BadRslot => StoreError::ReadTransactionAlreadyExists(::std::thread::current().id()),
83            e => StoreError::LmdbError(e),
84        }
85    }
86}
87
88impl From<DataError> for StoreError {
89    fn from(e: DataError) -> StoreError {
90        StoreError::DataError(e)
91    }
92}
93
94impl From<::std::io::Error> for StoreError {
95    fn from(e: ::std::io::Error) -> StoreError {
96        StoreError::IoError(e)
97    }
98}
99
100#[derive(Debug, Error)]
101pub enum MigrateError {
102    #[error("database not found: {0:?}")]
103    DatabaseNotFound(String),
104
105    #[error("{0}")]
106    FromString(String),
107
108    #[error("couldn't determine bit depth")]
109    IndeterminateBitDepth,
110
111    #[error("I/O error: {0:?}")]
112    IoError(::std::io::Error),
113
114    #[error("invalid DatabaseFlags bits")]
115    InvalidDatabaseBits,
116
117    #[error("invalid data version")]
118    InvalidDataVersion,
119
120    #[error("invalid magic number")]
121    InvalidMagicNum,
122
123    #[error("invalid NodeFlags bits")]
124    InvalidNodeBits,
125
126    #[error("invalid PageFlags bits")]
127    InvalidPageBits,
128
129    #[error("invalid page number")]
130    InvalidPageNum,
131
132    #[error("lmdb error: {0}")]
133    LmdbError(aingle_lmdb::Error),
134
135    #[error("string conversion error")]
136    StringConversionError,
137
138    #[error("TryFromInt error: {0:?}")]
139    TryFromIntError(::std::num::TryFromIntError),
140
141    #[error("unexpected Page variant")]
142    UnexpectedPageVariant,
143
144    #[error("unexpected PageHeader variant")]
145    UnexpectedPageHeaderVariant,
146
147    #[error("unsupported PageHeader variant")]
148    UnsupportedPageHeaderVariant,
149
150    #[error("UTF8 error: {0:?}")]
151    Utf8Error(::std::str::Utf8Error),
152}
153
154impl From<::std::io::Error> for MigrateError {
155    fn from(e: ::std::io::Error) -> MigrateError {
156        MigrateError::IoError(e)
157    }
158}
159
160impl From<::std::str::Utf8Error> for MigrateError {
161    fn from(e: ::std::str::Utf8Error) -> MigrateError {
162        MigrateError::Utf8Error(e)
163    }
164}
165
166impl From<::std::num::TryFromIntError> for MigrateError {
167    fn from(e: ::std::num::TryFromIntError) -> MigrateError {
168        MigrateError::TryFromIntError(e)
169    }
170}
171
172impl From<&str> for MigrateError {
173    fn from(e: &str) -> MigrateError {
174        MigrateError::FromString(e.to_string())
175    }
176}
177
178impl From<String> for MigrateError {
179    fn from(e: String) -> MigrateError {
180        MigrateError::FromString(e)
181    }
182}
183
184impl From<aingle_lmdb::Error> for MigrateError {
185    fn from(e: aingle_lmdb::Error) -> MigrateError {
186        match e {
187            e => MigrateError::LmdbError(e),
188        }
189    }
190}