transact/database/
error.rs

1/*
2 * Copyright 2018 Intel Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 * ------------------------------------------------------------------------------
16 */
17
18//! A common set of errors that can occur on database operations.
19
20#[derive(Debug)]
21pub enum DatabaseError {
22    InitError(String),
23    ReaderError(String),
24    WriterError(String),
25    CorruptionError(String),
26    NotFoundError(String),
27    DuplicateEntry,
28}
29
30impl std::fmt::Display for DatabaseError {
31    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
32        match *self {
33            DatabaseError::InitError(ref msg) => write!(f, "InitError: {}", msg),
34            DatabaseError::ReaderError(ref msg) => write!(f, "ReaderError: {}", msg),
35            DatabaseError::WriterError(ref msg) => write!(f, "WriterError: {}", msg),
36            DatabaseError::CorruptionError(ref msg) => write!(f, "CorruptionError: {}", msg),
37            DatabaseError::NotFoundError(ref msg) => write!(f, "NotFoundError: {}", msg),
38            DatabaseError::DuplicateEntry => write!(f, "DuplicateEntry"),
39        }
40    }
41}
42
43impl std::error::Error for DatabaseError {
44    fn description(&self) -> &str {
45        match *self {
46            DatabaseError::InitError(ref msg) => msg,
47            DatabaseError::ReaderError(ref msg) => msg,
48            DatabaseError::WriterError(ref msg) => msg,
49            DatabaseError::CorruptionError(ref msg) => msg,
50            DatabaseError::NotFoundError(ref msg) => msg,
51            DatabaseError::DuplicateEntry => "DuplicateEntry",
52        }
53    }
54
55    fn cause(&self) -> Option<&dyn std::error::Error> {
56        match *self {
57            DatabaseError::InitError(_) => None,
58            DatabaseError::ReaderError(_) => None,
59            DatabaseError::WriterError(_) => None,
60            DatabaseError::CorruptionError(_) => None,
61            DatabaseError::NotFoundError(_) => None,
62            DatabaseError::DuplicateEntry => None,
63        }
64    }
65}