fjall/
error.rs

1// Copyright (c) 2024-present, fjall-rs
2// This source code is licensed under both the Apache 2.0 and MIT License
3// (found in the LICENSE-* files in the repository)
4
5use crate::{journal::error::RecoveryError as JournalRecoveryError, version::Version};
6use lsm_tree::{DecodeError, EncodeError};
7
8/// Errors that may occur in the storage engine
9#[derive(Debug)]
10#[non_exhaustive]
11pub enum Error {
12    /// Error inside LSM-tree
13    Storage(lsm_tree::Error),
14
15    /// I/O error
16    Io(std::io::Error),
17
18    /// Serialization failed
19    Encode(EncodeError),
20
21    /// Deserialization failed
22    Decode(DecodeError),
23
24    /// Error during journal recovery
25    JournalRecovery(JournalRecoveryError),
26
27    /// Invalid or unparsable data format version
28    InvalidVersion(Option<Version>),
29
30    /// A previous flush / commit operation failed, indicating a hardware-related failure
31    ///
32    /// Future writes will not be accepted as consistency cannot be guaranteed.
33    ///
34    /// **At this point, it's best to let the application crash and try to recover.**
35    ///
36    /// More info: <https://www.usenix.org/system/files/atc20-rebello.pdf>
37    Poisoned,
38
39    /// Partition is deleted
40    PartitionDeleted,
41}
42
43impl std::fmt::Display for Error {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        write!(f, "FjallError: {self:?}")
46    }
47}
48
49impl From<std::io::Error> for Error {
50    fn from(inner: std::io::Error) -> Self {
51        Self::Io(inner)
52    }
53}
54
55impl From<EncodeError> for Error {
56    fn from(value: EncodeError) -> Self {
57        Self::Encode(value)
58    }
59}
60
61impl From<DecodeError> for Error {
62    fn from(value: DecodeError) -> Self {
63        Self::Decode(value)
64    }
65}
66
67impl From<lsm_tree::Error> for Error {
68    fn from(inner: lsm_tree::Error) -> Self {
69        Self::Storage(inner)
70    }
71}
72
73impl std::error::Error for Error {
74    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
75        match self {
76            Self::Storage(inner) => Some(inner),
77            Self::Io(inner) => Some(inner),
78            Self::Encode(inner) => Some(inner),
79            Self::Decode(inner) => Some(inner),
80            Self::JournalRecovery(inner) => Some(inner),
81            Self::InvalidVersion(_) => None,
82            Self::Poisoned => None,
83            Self::PartitionDeleted => None,
84        }
85    }
86}
87
88/// Result helper type
89pub type Result<T> = std::result::Result<T, Error>;