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, Serialize)]
12pub enum StorageError {
13    /// I/O error.
14    #[error("{0}")]
15    #[serde(serialize_with = "serialize_io_error")]
16    StdIo(ErrorKind),
17
18    /// A process already locked the provided storage directory.
19    ///
20    /// If this is not expected, please remove the lock file manually, after verifying
21    /// that the process with the given PID no longer exists.
22    #[error("A process with PID {0} is already using the storage directory {1:?}.")]
23    StorageLocked(u32, PathBuf),
24
25    /// Unknown checkpoint specified in configuration.
26    #[error("Couldn't find the specified checkpoint ({0:?}).")]
27    CheckpointNotFound(Uuid),
28
29    /// The operator wasn't assigned a persistent ID when the circuit was constructed.
30    #[error("Internal error: operator {0} has not been assigned a persistent id.")]
31    NoPersistentId(String),
32
33    /// Cannot perform operation because storage is not enabled.
34    #[error("Cannot perform operation because storage is not enabled.")]
35    StorageDisabled,
36    /// Error while creating a bloom filter.
37    #[error("Failed to serialize/deserialize bloom filter.")]
38    BloomFilter,
39
40    /// Path is not valid in storage.
41    ///
42    /// Storage paths may not be absolute, may not start with a drive letter (on
43    /// Windows), and may not contain `.` or `..` components.
44    #[error("Path is not valid in storage: {}", .0.display())]
45    InvalidPath(PathBuf),
46
47    /// Unable to parse URL.
48    #[error("Unable to parse URL {0:?}")]
49    InvalidURL(String),
50
51    /// Error accessing object store.
52    #[error("Error accessing object store: {message}")]
53    #[serde(serialize_with = "serialize_object_store_error")]
54    ObjectStore { kind: ErrorKind, message: String },
55
56    /// The requested storage backend is not available.
57    #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
58    )]
59    BackendNotSupported(Box<StorageBackendConfig>),
60
61    /// The requested storage backend ({backend}) cannot be configured with {}.
62    #[error("The requested storage backend ({backend}) cannot be configured with {config:?}.")]
63    InvalidBackendConfig {
64        backend: String,
65        config: StorageBackendConfig,
66    },
67}
68
69impl From<std::io::Error> for StorageError {
70    fn from(value: std::io::Error) -> Self {
71        Self::StdIo(value.kind())
72    }
73}
74
75impl From<ObjectStoreError> for StorageError {
76    fn from(value: ObjectStoreError) -> Self {
77        let kind = match value {
78            ObjectStoreError::NotFound { .. } => ErrorKind::NotFound,
79            ObjectStoreError::NotSupported { .. } => ErrorKind::Unsupported,
80            ObjectStoreError::AlreadyExists { .. } => ErrorKind::AlreadyExists,
81            ObjectStoreError::NotImplemented => ErrorKind::Unsupported,
82            ObjectStoreError::PermissionDenied { .. }
83            | ObjectStoreError::Unauthenticated { .. } => ErrorKind::PermissionDenied,
84            ObjectStoreError::InvalidPath { .. } => {
85                // Should be `ErrorKind::InvalidFilename` (once stabilized).
86                ErrorKind::Other
87            }
88            ObjectStoreError::Generic { .. }
89            | ObjectStoreError::JoinError { .. }
90            | ObjectStoreError::Precondition { .. }
91            | ObjectStoreError::NotModified { .. }
92            | ObjectStoreError::UnknownConfigurationKey { .. }
93            | _ => ErrorKind::Other,
94        };
95        Self::ObjectStore {
96            kind,
97            message: value.to_string(),
98        }
99    }
100}
101
102fn serialize_io_error<S>(kind: &ErrorKind, serializer: S) -> Result<S::Ok, S::Error>
103where
104    S: Serializer,
105{
106    let mut ser = serializer.serialize_struct("IOError", 1)?;
107    ser.serialize_field("kind", &kind.to_string())?;
108    ser.end()
109}
110
111fn serialize_object_store_error<S>(
112    kind: &ErrorKind,
113    message: &String,
114    serializer: S,
115) -> Result<S::Ok, S::Error>
116where
117    S: Serializer,
118{
119    let mut ser = serializer.serialize_struct("ObjectStoreError", 2)?;
120    ser.serialize_field("kind", &kind.to_string())?;
121    ser.serialize_field("message", message)?;
122    ser.end()
123}
124
125impl StorageError {
126    pub fn kind(&self) -> ErrorKind {
127        match self {
128            StorageError::StdIo(kind) => *kind,
129            StorageError::StorageLocked(..) => ErrorKind::ResourceBusy,
130            StorageError::NoPersistentId(_) => ErrorKind::Other,
131            StorageError::CheckpointNotFound(_) => ErrorKind::NotFound,
132            StorageError::StorageDisabled => ErrorKind::Other,
133            StorageError::BloomFilter => ErrorKind::Other,
134            StorageError::InvalidPath(_) => ErrorKind::Other,
135            StorageError::InvalidURL(_) => ErrorKind::Other,
136            StorageError::ObjectStore { kind, .. } => *kind,
137            StorageError::BackendNotSupported(_) => ErrorKind::Other,
138            StorageError::InvalidBackendConfig { .. } => ErrorKind::Other,
139        }
140    }
141
142    pub fn ignore_notfound<T>(result: Result<T, Self>) -> Result<(), Self> {
143        match result {
144            Ok(_) => Ok(()),
145            Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
146            Err(error) => Err(error),
147        }
148    }
149}