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    /// The operator wasn't assigned a persistent ID when the circuit was constructed.
29    #[error("Internal error: operator {0} has not been assigned a persistent id.")]
30    NoPersistentId(String),
31
32    /// Cannot perform operation because storage is not enabled.
33    #[error("Cannot perform operation because storage is not enabled.")]
34    StorageDisabled,
35    /// Error while creating a bloom filter.
36    #[error("Failed to serialize/deserialize bloom filter.")]
37    BloomFilter,
38
39    /// Path is not valid in storage.
40    ///
41    /// Storage paths may not be absolute, may not start with a drive letter (on
42    /// Windows), and may not contain `.` or `..` components.
43    #[error("Path is not valid in storage: {}", .0.display())]
44    InvalidPath(PathBuf),
45
46    /// Unable to parse URL.
47    #[error("Unable to parse URL {0:?}")]
48    InvalidURL(String),
49
50    /// Error accessing object store.
51    #[error("Error accessing object store: {message}")]
52    ObjectStore { kind: ErrorKind, message: String },
53
54    /// The requested storage backend is not available.
55    #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
56    )]
57    BackendNotSupported(StorageBackendConfig),
58}
59
60impl From<std::io::Error> for StorageError {
61    fn from(value: std::io::Error) -> Self {
62        Self::StdIo(value.kind())
63    }
64}
65
66impl From<ObjectStoreError> for StorageError {
67    fn from(value: ObjectStoreError) -> Self {
68        let kind = match value {
69            ObjectStoreError::NotFound { .. } => ErrorKind::NotFound,
70            ObjectStoreError::NotSupported { .. } => ErrorKind::Unsupported,
71            ObjectStoreError::AlreadyExists { .. } => ErrorKind::AlreadyExists,
72            ObjectStoreError::NotImplemented => ErrorKind::Unsupported,
73            ObjectStoreError::PermissionDenied { .. }
74            | ObjectStoreError::Unauthenticated { .. } => ErrorKind::PermissionDenied,
75            ObjectStoreError::InvalidPath { .. } => {
76                // Should be `ErrorKind::InvalidFilename` (once stabilized).
77                ErrorKind::Other
78            }
79            ObjectStoreError::Generic { .. }
80            | ObjectStoreError::JoinError { .. }
81            | ObjectStoreError::Precondition { .. }
82            | ObjectStoreError::NotModified { .. }
83            | ObjectStoreError::UnknownConfigurationKey { .. }
84            | _ => ErrorKind::Other,
85        };
86        Self::ObjectStore {
87            kind,
88            message: value.to_string(),
89        }
90    }
91}
92
93impl Serialize for StorageError {
94    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
95    where
96        S: Serializer,
97    {
98        match self {
99            Self::StdIo(error) => {
100                let mut ser = serializer.serialize_struct("IOError", 1)?;
101                ser.serialize_field("kind", &error.to_string())?;
102                ser.end()
103            }
104            error => error.serialize(serializer),
105        }
106    }
107}
108
109impl StorageError {
110    pub fn kind(&self) -> ErrorKind {
111        match self {
112            StorageError::StdIo(kind) => *kind,
113            StorageError::StorageLocked(..) => ErrorKind::ResourceBusy,
114            StorageError::NoPersistentId(_) => ErrorKind::Other,
115            StorageError::CheckpointNotFound(_) => ErrorKind::NotFound,
116            StorageError::StorageDisabled => ErrorKind::Other,
117            StorageError::BloomFilter => ErrorKind::Other,
118            StorageError::InvalidPath(_) => ErrorKind::Other,
119            StorageError::InvalidURL(_) => ErrorKind::Other,
120            StorageError::ObjectStore { kind, .. } => *kind,
121            StorageError::BackendNotSupported(_) => ErrorKind::Other,
122        }
123    }
124
125    pub fn ignore_notfound<T>(result: Result<T, Self>) -> Result<(), Self> {
126        match result {
127            Ok(_) => Ok(()),
128            Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
129            Err(error) => Err(error),
130        }
131    }
132}