tsdb-reader 0.2.0

Read-only, zero-copy Prometheus TSDB block reader: index v1-v3, all chunk encodings, integer histograms, postings lookups, tombstones
Documentation
//! Error type shared by every parser in the crate.

use std::fmt;

/// Result alias used throughout the crate.
pub type Result<T> = std::result::Result<T, Error>;

/// Everything that can go wrong while reading a block.
///
/// The variants are deliberately coarse: callers usually only need to tell
/// "the data is corrupt" from "the file is not what we expected" from "the I/O
/// layer failed". All of them are terminal for a single block, so the usual
/// response is to skip or quarantine that block and continue.
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Wraps a filesystem or memory-mapping failure.
    #[error("io error: {0}")]
    Io(#[from] std::io::Error),
    /// The file does not follow the Prometheus on-disk layout.
    #[error("invalid format: {0}")]
    Format(String),
    /// CRC32C mismatch in the named section (`index toc`, `index series`,
    /// `index symbols`, `chunk` or `tombstones`).
    #[error("checksum mismatch: {0}")]
    Checksum(&'static str),
    /// A read ran past the end of the buffer, at the given absolute offset.
    #[error("unexpected end of data at byte {0}")]
    Eof(usize),
    /// A chunk payload could not be decoded.
    #[error("chunk decode error: {0}")]
    Decode(String),
    /// The data contradicts itself: a checksum is fine but the structure, the
    /// metadata or the cross-references do not add up.
    #[error("corrupt data: {0}")]
    Corrupt(String),
    /// A format version or chunk encoding this crate does not implement.
    #[error("unsupported: {0}")]
    Unsupported(String),
}

impl Error {
    /// Builds [`Error::Format`] from anything displayable.
    pub fn format(msg: impl fmt::Display) -> Self {
        Error::Format(msg.to_string())
    }

    /// Builds [`Error::Decode`] from anything displayable.
    pub fn decode(msg: impl fmt::Display) -> Self {
        Error::Decode(msg.to_string())
    }

    /// Builds [`Error::Corrupt`] from anything displayable.
    pub fn corrupt(msg: impl fmt::Display) -> Self {
        Error::Corrupt(msg.to_string())
    }

    /// True when the bytes are malformed or inconsistent.
    ///
    /// The usual response is to skip the block and keep going; IO and
    /// unsupported-version errors are not covered.
    pub fn is_corruption(&self) -> bool {
        matches!(
            self,
            Error::Format(_)
                | Error::Checksum(_)
                | Error::Eof(_)
                | Error::Decode(_)
                | Error::Corrupt(_)
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn constructors_keep_the_message() {
        assert!(matches!(Error::format("x"), Error::Format(ref m) if m == "x"));
        assert!(matches!(Error::decode("y"), Error::Decode(ref m) if m == "y"));
        assert!(matches!(Error::corrupt("z"), Error::Corrupt(ref m) if m == "z"));
    }

    #[test]
    fn corruption_is_classified() {
        assert!(Error::format("x").is_corruption());
        assert!(Error::corrupt("x").is_corruption());
        assert!(Error::Eof(3).is_corruption());
        assert!(!Error::Unsupported("v9".into()).is_corruption());
        assert!(!Error::from(std::io::Error::other("disk")).is_corruption());
    }

    #[test]
    fn io_errors_keep_their_source() {
        let error = Error::from(std::io::Error::new(std::io::ErrorKind::NotFound, "gone"));
        assert!(matches!(error, Error::Io(_)));
        assert!(std::error::Error::source(&error).is_some());
        assert!(error.to_string().contains("gone"));
    }
}