epoch_db/db/
errors.rs

1//! This module defines the custom error types used throughout the EpochDB
2//! library.
3use std::error::Error;
4use std::fmt::Display;
5use std::path::PathBuf;
6
7/// The primary error enum for the EpochDB library.
8/// Fun Fact: It's called TransientError because Transient is the old name of
9/// the DB
10#[derive(Debug)]
11pub enum TransientError {
12    /// Error that occurs during frequency increment operations.
13    IncretmentError,
14    /// Error that occurs when parsing to a byte slice fails.
15    ParsingToByteError,
16    /// Error that occurs when parsing to a UTF-8 string fails.
17    ParsingToUTF8Error,
18    /// Wrapper for `sled::Error`.
19    SledError {
20        /// The underlying `sled` error.
21        error: sled::Error
22    },
23    /// Error that occurs during a `sled` transaction.
24    SledTransactionError,
25    /// Error that occurs when parsing a byte slice to a u64 fails.
26    ParsingToU64ByteFailed,
27    /// Error that occurs when any folder in the path doesnt exist.
28    FolderNotFound { path: PathBuf },
29    /// Wrapper for `zip::result::ZipError`.
30    ZipError {
31        /// The underlying `zip::result::ZipError`.
32        error: zip::result::ZipError
33    },
34    /// Error that occurs when the file doesnt exist.
35    FileNameDoesntExist,
36    /// Error that occurs when the corresponding Metadata doesnt exist.
37    MetadataNotFound,
38    /// Error that occurs when the Metadata of the database itself doesnt exist.
39    DBMetadataNotFound,
40    /// Error that occurs when a Mutex is poisoned.
41    PoisonedMutex,
42    /// Error that occurs when parsing from a byte slice to any type.
43    ParsingFromByteError,
44    /// Wrapper for `std::io::Error`.
45    IOError {
46        /// The underlying `std::io::Error`
47        error: std::io::Error
48    }
49}
50
51impl Display for TransientError {
52    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
53        match self {
54            TransientError::IncretmentError => writeln!(f, "Incretment has failed"),
55            TransientError::ParsingToByteError => writeln!(f, "Parsing to byte failed"),
56            TransientError::ParsingToUTF8Error => writeln!(f, "Parsing to utf8 failed"),
57            TransientError::SledError {
58                error
59            } => writeln!(f, "Sled failed {error}"),
60            TransientError::SledTransactionError => writeln!(f, "Sled Transaction failed"),
61            TransientError::ParsingToU64ByteFailed => {
62                writeln!(f, "Failed to parse a variable to a U64 byte [u8; 8]")
63            },
64            TransientError::FolderNotFound {
65                path
66            } => {
67                writeln!(f, "Folder is not found at the path: {path:#?}")
68            },
69            TransientError::ZipError {
70                error
71            } => writeln!(f, "Zip crate failed {error}"),
72            TransientError::FileNameDoesntExist => writeln!(f, "File name doesnt exist"),
73            TransientError::MetadataNotFound => writeln!(f, "Metadata is not found"),
74            TransientError::DBMetadataNotFound => writeln!(f, "DB metadata is not found"),
75            TransientError::PoisonedMutex => writeln!(f, "Mutex is poisoned"),
76            TransientError::ParsingFromByteError => writeln!(f, "Partsing from byte failed"),
77            TransientError::IOError {
78                error
79            } => writeln!(f, "std IO failed {error}")
80        }
81    }
82}
83
84impl Error for TransientError {}