Skip to main content

tsdb_reader/
error.rs

1//! Error type shared by every parser in the crate.
2
3use std::fmt;
4
5/// Result alias used throughout the crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// Everything that can go wrong while reading a block.
9///
10/// The variants are deliberately coarse: callers usually only need to tell
11/// "the data is corrupt" from "the file is not what we expected" from "the I/O
12/// layer failed". All of them are terminal for a single block, so the usual
13/// response is to skip or quarantine that block and continue.
14#[derive(Debug, thiserror::Error)]
15pub enum Error {
16    /// Wraps a filesystem or memory-mapping failure.
17    #[error("io error: {0}")]
18    Io(#[from] std::io::Error),
19    /// The file does not follow the Prometheus on-disk layout.
20    #[error("invalid format: {0}")]
21    Format(String),
22    /// CRC32C mismatch in the named section (`index toc`, `index series`,
23    /// `index symbols`, `chunk` or `tombstones`).
24    #[error("checksum mismatch: {0}")]
25    Checksum(&'static str),
26    /// A read ran past the end of the buffer, at the given absolute offset.
27    #[error("unexpected end of data at byte {0}")]
28    Eof(usize),
29    /// A chunk payload could not be decoded.
30    #[error("chunk decode error: {0}")]
31    Decode(String),
32    /// The data contradicts itself: a checksum is fine but the structure, the
33    /// metadata or the cross-references do not add up.
34    #[error("corrupt data: {0}")]
35    Corrupt(String),
36    /// A format version or chunk encoding this crate does not implement.
37    #[error("unsupported: {0}")]
38    Unsupported(String),
39}
40
41impl Error {
42    /// Builds [`Error::Format`] from anything displayable.
43    pub fn format(msg: impl fmt::Display) -> Self {
44        Error::Format(msg.to_string())
45    }
46
47    /// Builds [`Error::Decode`] from anything displayable.
48    pub fn decode(msg: impl fmt::Display) -> Self {
49        Error::Decode(msg.to_string())
50    }
51
52    /// Builds [`Error::Corrupt`] from anything displayable.
53    pub fn corrupt(msg: impl fmt::Display) -> Self {
54        Error::Corrupt(msg.to_string())
55    }
56
57    /// True when the bytes are malformed or inconsistent.
58    ///
59    /// The usual response is to skip the block and keep going; IO and
60    /// unsupported-version errors are not covered.
61    pub fn is_corruption(&self) -> bool {
62        matches!(
63            self,
64            Error::Format(_)
65                | Error::Checksum(_)
66                | Error::Eof(_)
67                | Error::Decode(_)
68                | Error::Corrupt(_)
69        )
70    }
71}
72
73#[cfg(test)]
74mod tests {
75    use super::*;
76
77    #[test]
78    fn constructors_keep_the_message() {
79        assert!(matches!(Error::format("x"), Error::Format(ref m) if m == "x"));
80        assert!(matches!(Error::decode("y"), Error::Decode(ref m) if m == "y"));
81        assert!(matches!(Error::corrupt("z"), Error::Corrupt(ref m) if m == "z"));
82    }
83
84    #[test]
85    fn corruption_is_classified() {
86        assert!(Error::format("x").is_corruption());
87        assert!(Error::corrupt("x").is_corruption());
88        assert!(Error::Eof(3).is_corruption());
89        assert!(!Error::Unsupported("v9".into()).is_corruption());
90        assert!(!Error::from(std::io::Error::other("disk")).is_corruption());
91    }
92
93    #[test]
94    fn io_errors_keep_their_source() {
95        let error = Error::from(std::io::Error::new(std::io::ErrorKind::NotFound, "gone"));
96        assert!(matches!(error, Error::Io(_)));
97        assert!(std::error::Error::source(&error).is_some());
98        assert!(error.to_string().contains("gone"));
99    }
100}