lsm_tree/
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::{Checksum, CompressionType};
6
7/// Represents errors that can occur in the LSM-tree
8#[derive(Debug)]
9#[non_exhaustive]
10pub enum Error {
11    /// I/O error
12    Io(std::io::Error),
13
14    /// Decompression failed
15    Decompress(CompressionType),
16
17    /// Invalid or unparsable data format version
18    InvalidVersion(u8),
19
20    /// Some required files could not be recovered from disk
21    Unrecoverable,
22
23    /// Checksum mismatch
24    ChecksumMismatch {
25        /// Checksum of loaded block
26        got: Checksum,
27
28        /// Checksum that was saved in block header
29        expected: Checksum,
30    },
31
32    /// Invalid enum tag
33    InvalidTag((&'static str, u8)),
34
35    /// Invalid block trailer
36    InvalidTrailer,
37
38    /// Invalid block header
39    InvalidHeader(&'static str),
40
41    /// UTF-8 error
42    Utf8(std::str::Utf8Error),
43}
44
45#[cfg_attr(test, mutants::skip)]
46impl std::fmt::Display for Error {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        write!(f, "LsmTreeError: {self:?}")
49    }
50}
51
52impl std::error::Error for Error {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match self {
55            Self::Io(e) => Some(e),
56            _ => None,
57        }
58    }
59}
60
61impl From<sfa::Error> for Error {
62    fn from(value: sfa::Error) -> Self {
63        match value {
64            sfa::Error::Io(e) => Self::from(e),
65            sfa::Error::ChecksumMismatch { got, expected } => {
66                log::error!("Archive ToC checksum mismatch");
67                Self::ChecksumMismatch {
68                    got: got.into(),
69                    expected: expected.into(),
70                }
71            }
72            sfa::Error::InvalidHeader => {
73                log::error!("Invalid archive header");
74                Self::Unrecoverable
75            }
76            sfa::Error::InvalidVersion => {
77                log::error!("Invalid archive version");
78                Self::Unrecoverable
79            }
80            sfa::Error::UnsupportedChecksumType => {
81                log::error!("Invalid archive checksum type");
82                Self::Unrecoverable
83            }
84        }
85    }
86}
87
88impl From<std::io::Error> for Error {
89    fn from(value: std::io::Error) -> Self {
90        Self::Io(value)
91    }
92}
93
94/// Tree result
95pub type Result<T> = std::result::Result<T, Error>;