Skip to main content

timeseries_table_format/storage/
error.rs

1use std::{error::Error, fmt, io};
2
3use snafu::{Backtrace, prelude::*};
4
5/// Errors produced by the storage backend implementation.
6///
7/// Currently this crate only supports a local filesystem backend; backend-specific
8/// I/O errors are wrapped in this enum so higher layers can map them into
9/// StorageError variants with additional context.
10#[derive(Debug)]
11pub enum BackendError {
12    /// A local filesystem I/O error.
13    Local(io::Error),
14}
15
16impl fmt::Display for BackendError {
17    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
18        match self {
19            BackendError::Local(e) => write!(f, "local I/O error: {e}"),
20        }
21    }
22}
23
24impl Error for BackendError {
25    fn source(&self) -> Option<&(dyn Error + 'static)> {
26        match self {
27            BackendError::Local(e) => Some(e),
28        }
29    }
30}
31
32/// Errors that can occur during storage operations.
33#[derive(Debug, Snafu)]
34#[snafu(visibility(pub(crate)))]
35pub enum StorageError {
36    /// The specified path was not found.
37    #[snafu(display("Path not found: {path}"))]
38    NotFound {
39        /// The path that was not found.
40        path: String,
41        /// Underlying backend error that caused the failure.
42        source: BackendError,
43        /// The backtrace at the time the error occurred.
44        backtrace: Backtrace,
45    },
46
47    /// The specified path already exists when creation was requested with
48    /// create-new semantics.
49    #[snafu(display("Path already exists: {path}"))]
50    AlreadyExists {
51        /// The path that was found to already exist.
52        path: String,
53        /// Underlying backend error that indicates the existing resource.
54        source: BackendError,
55        /// The backtrace captured when the error occurred.
56        backtrace: Backtrace,
57    },
58
59    /// An I/O error occurred on the local filesystem.
60    #[snafu(display("Local I/O error at {path}: {source}"))]
61    OtherIo {
62        /// The path where the I/O error occurred.
63        path: String,
64        /// Underlying backend I/O error with platform-specific details.
65        source: BackendError,
66        /// The backtrace at the time the error occurred.
67        backtrace: Backtrace,
68    },
69
70    /// A newly-created object could not be removed after its write failed.
71    #[snafu(display(
72        "Storage operation failed at {path}: {operation_error}; cleanup also failed: {cleanup_error}"
73    ))]
74    CleanupFailed {
75        /// Path of the object that may remain after the failed cleanup.
76        path: String,
77        /// Original write or sync failure.
78        #[snafu(source)]
79        operation_error: Box<StorageError>,
80        /// Failure encountered while removing the newly-created object.
81        cleanup_error: Box<StorageError>,
82        /// The backtrace at the time the cleanup failure was reported.
83        backtrace: Backtrace,
84    },
85}