feldera_storage/
error.rs

1use feldera_types::config::StorageBackendConfig;
2use object_store::Error as ObjectStoreError;
3use serde::ser::SerializeStruct;
4use serde::{Serialize, Serializer};
5use std::io::ErrorKind;
6use std::path::PathBuf;
7use thiserror::Error;
8use uuid::Uuid;
9
10/// An error that can occur when using the storage backend.
11#[derive(Clone, Error, Debug)]
12pub enum StorageError {
13    /// I/O error.
14    #[error("{0}")]
15    StdIo(ErrorKind),
16
17    /// A process already locked the provided storage directory.
18    ///
19    /// If this is not expected, please remove the lock file manually, after verifying
20    /// that the process with the given PID no longer exists.
21    #[error("A process with PID {0} is already using the storage directory {1:?}.")]
22    StorageLocked(u32, PathBuf),
23
24    /// Unknown checkpoint specified in configuration.
25    #[error("Couldn't find the specified checkpoint ({0:?}).")]
26    CheckpointNotFound(Uuid),
27
28    /// Cannot perform operation because storage is not enabled.
29    #[error("Cannot perform operation because storage is not enabled.")]
30    StorageDisabled,
31    /// Error while creating a bloom filter.
32    #[error("Failed to serialize/deserialize bloom filter.")]
33    BloomFilter,
34
35    /// Path is not valid in storage.
36    ///
37    /// Storage paths may not be absolute, may not start with a drive letter (on
38    /// Windows), and may not contain `.` or `..` components.
39    #[error("Path is not valid in storage: {}", .0.display())]
40    InvalidPath(PathBuf),
41
42    /// Unable to parse URL.
43    #[error("Unable to parse URL {0:?}")]
44    InvalidURL(String),
45
46    /// Error accessing object store.
47    #[error("Error accessing object store: {message}")]
48    ObjectStore { kind: ErrorKind, message: String },
49
50    /// The requested storage backend is not available.
51    #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
52    )]
53    BackendNotSupported(StorageBackendConfig),
54}
55
56impl From<std::io::Error> for StorageError {
57    fn from(value: std::io::Error) -> Self {
58        Self::StdIo(value.kind())
59    }
60}
61
62impl From<ObjectStoreError> for StorageError {
63    fn from(value: ObjectStoreError) -> Self {
64        let kind = match value {
65            ObjectStoreError::NotFound { .. } => ErrorKind::NotFound,
66            ObjectStoreError::NotSupported { .. } => ErrorKind::Unsupported,
67            ObjectStoreError::AlreadyExists { .. } => ErrorKind::AlreadyExists,
68            ObjectStoreError::NotImplemented => ErrorKind::Unsupported,
69            ObjectStoreError::PermissionDenied { .. }
70            | ObjectStoreError::Unauthenticated { .. } => ErrorKind::PermissionDenied,
71            ObjectStoreError::InvalidPath { .. } => {
72                // Should be `ErrorKind::InvalidFilename` (once stabilized).
73                ErrorKind::Other
74            }
75            ObjectStoreError::Generic { .. }
76            | ObjectStoreError::JoinError { .. }
77            | ObjectStoreError::Precondition { .. }
78            | ObjectStoreError::NotModified { .. }
79            | ObjectStoreError::UnknownConfigurationKey { .. }
80            | _ => ErrorKind::Other,
81        };
82        Self::ObjectStore {
83            kind,
84            message: value.to_string(),
85        }
86    }
87}
88
89impl Serialize for StorageError {
90    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
91    where
92        S: Serializer,
93    {
94        match self {
95            Self::StdIo(error) => {
96                let mut ser = serializer.serialize_struct("IOError", 1)?;
97                ser.serialize_field("kind", &error.to_string())?;
98                ser.end()
99            }
100            error => error.serialize(serializer),
101        }
102    }
103}
104
105impl StorageError {
106    pub fn kind(&self) -> ErrorKind {
107        match self {
108            StorageError::StdIo(kind) => *kind,
109            StorageError::StorageLocked(..) => ErrorKind::ResourceBusy,
110            StorageError::CheckpointNotFound(_) => ErrorKind::NotFound,
111            StorageError::StorageDisabled => ErrorKind::Other,
112            StorageError::BloomFilter => ErrorKind::Other,
113            StorageError::InvalidPath(_) => ErrorKind::Other,
114            StorageError::InvalidURL(_) => ErrorKind::Other,
115            StorageError::ObjectStore { kind, .. } => *kind,
116            StorageError::BackendNotSupported(_) => ErrorKind::Other,
117        }
118    }
119}