Skip to main content

dodb_core/
error.rs

1use std::fmt;
2
3/// Recoverable errors shared by semantic, storage, and test-harness code.
4#[derive(Debug)]
5pub enum Error {
6    InvalidInput(String),
7    InvalidRequest(String),
8    Overloaded(String),
9    ResponseTooLarge(String),
10    Conflict(crate::transaction::TransactionConflict),
11    Corruption(String),
12    Io(std::io::Error),
13    UnsupportedFormat(String),
14    DurabilityFailure(String),
15    RecoveryFailure(String),
16    CheckpointFailure(String),
17    InternalInvariantViolation(String),
18}
19
20pub type Result<T> = std::result::Result<T, Error>;
21
22impl Error {
23    pub fn invalid_input(message: impl Into<String>) -> Self {
24        Self::InvalidInput(message.into())
25    }
26
27    pub fn corruption(message: impl Into<String>) -> Self {
28        Self::Corruption(message.into())
29    }
30
31    pub fn invalid_request(message: impl Into<String>) -> Self {
32        Self::InvalidRequest(message.into())
33    }
34
35    pub fn overloaded(message: impl Into<String>) -> Self {
36        Self::Overloaded(message.into())
37    }
38
39    pub fn response_too_large(message: impl Into<String>) -> Self {
40        Self::ResponseTooLarge(message.into())
41    }
42
43    pub fn conflict(conflict: crate::transaction::TransactionConflict) -> Self {
44        Self::Conflict(conflict)
45    }
46
47    pub fn unsupported_format(message: impl Into<String>) -> Self {
48        Self::UnsupportedFormat(message.into())
49    }
50
51    pub fn durability(message: impl Into<String>) -> Self {
52        Self::DurabilityFailure(message.into())
53    }
54
55    pub fn recovery(message: impl Into<String>) -> Self {
56        Self::RecoveryFailure(message.into())
57    }
58
59    pub fn checkpoint(message: impl Into<String>) -> Self {
60        Self::CheckpointFailure(message.into())
61    }
62
63    pub fn invariant(message: impl Into<String>) -> Self {
64        Self::InternalInvariantViolation(message.into())
65    }
66}
67
68impl fmt::Display for Error {
69    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70        match self {
71            Self::InvalidInput(message) => write!(formatter, "invalid input: {message}"),
72            Self::InvalidRequest(message) => write!(formatter, "invalid request: {message}"),
73            Self::Overloaded(message) => write!(formatter, "overloaded: {message}"),
74            Self::ResponseTooLarge(message) => write!(formatter, "response too large: {message}"),
75            Self::Conflict(conflict) => write!(formatter, "conflict: {conflict}"),
76            Self::Corruption(message) => write!(formatter, "corruption: {message}"),
77            Self::Io(error) => write!(formatter, "I/O error: {error}"),
78            Self::UnsupportedFormat(message) => write!(formatter, "unsupported format: {message}"),
79            Self::DurabilityFailure(message) => write!(formatter, "durability failure: {message}"),
80            Self::RecoveryFailure(message) => write!(formatter, "recovery failure: {message}"),
81            Self::CheckpointFailure(message) => {
82                write!(formatter, "checkpoint failure: {message}")
83            }
84            Self::InternalInvariantViolation(message) => {
85                write!(formatter, "internal invariant violation: {message}")
86            }
87        }
88    }
89}
90
91impl std::error::Error for Error {
92    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
93        match self {
94            Self::Io(error) => Some(error),
95            _ => None,
96        }
97    }
98}
99
100impl From<std::io::Error> for Error {
101    fn from(error: std::io::Error) -> Self {
102        Self::Io(error)
103    }
104}