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#[derive(Clone, Error, Debug, Serialize)]
12pub enum StorageError {
13 #[error("{}: {operation} failed: {kind}", path.as_ref().map_or("(unknown file)", |path| path.as_str()))]
15 #[serde(serialize_with = "serialize_io_error")]
16 StdIo {
17 kind: ErrorKind,
18 operation: &'static str,
19 path: Option<String>,
20 },
21
22 #[error("A process with PID {0} is already using the storage directory {1:?}.")]
27 StorageLocked(u32, PathBuf),
28
29 #[error("Couldn't find the specified checkpoint ({0:?}).")]
31 CheckpointNotFound(Uuid),
32
33 #[error("Internal error: operator {0} has not been assigned a persistent id.")]
35 NoPersistentId(String),
36
37 #[error("Cannot perform operation because storage is not enabled.")]
39 StorageDisabled,
40 #[error("Failed to serialize/deserialize bloom filter.")]
42 BloomFilter,
43
44 #[error("Path is not valid in storage: {}", .0.display())]
49 InvalidPath(PathBuf),
50
51 #[error("Unable to parse URL {0:?}")]
53 InvalidURL(String),
54
55 #[error("Error accessing object store: {message}")]
57 #[serde(serialize_with = "serialize_object_store_error")]
58 ObjectStore { kind: ErrorKind, message: String },
59
60 #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
62 )]
63 BackendNotSupported(Box<StorageBackendConfig>),
64
65 #[error("The requested storage backend ({backend}) cannot be configured with {config:?}.")]
67 InvalidBackendConfig {
68 backend: String,
69 config: Box<StorageBackendConfig>,
70 },
71
72 #[error("Error deserializing JSON: {0}")]
73 JsonError(String),
74}
75
76impl From<ObjectStoreError> for StorageError {
77 fn from(value: ObjectStoreError) -> Self {
78 let kind = match value {
79 ObjectStoreError::NotFound { .. } => ErrorKind::NotFound,
80 ObjectStoreError::NotSupported { .. } => ErrorKind::Unsupported,
81 ObjectStoreError::AlreadyExists { .. } => ErrorKind::AlreadyExists,
82 ObjectStoreError::NotImplemented => ErrorKind::Unsupported,
83 ObjectStoreError::PermissionDenied { .. }
84 | ObjectStoreError::Unauthenticated { .. } => ErrorKind::PermissionDenied,
85 ObjectStoreError::InvalidPath { .. } => {
86 ErrorKind::Other
88 }
89 ObjectStoreError::Generic { .. }
90 | ObjectStoreError::JoinError { .. }
91 | ObjectStoreError::Precondition { .. }
92 | ObjectStoreError::NotModified { .. }
93 | ObjectStoreError::UnknownConfigurationKey { .. }
94 | _ => ErrorKind::Other,
95 };
96 Self::ObjectStore {
97 kind,
98 message: value.to_string(),
99 }
100 }
101}
102
103fn serialize_io_error<S>(
104 kind: &ErrorKind,
105 operation: &str,
106 path: &Option<String>,
107 serializer: S,
108) -> Result<S::Ok, S::Error>
109where
110 S: Serializer,
111{
112 let mut ser = serializer.serialize_struct("IOError", 3)?;
113 ser.serialize_field("kind", &kind.to_string())?;
114 ser.serialize_field("operation", operation)?;
115 ser.serialize_field("path", &path)?;
116 ser.end()
117}
118
119fn serialize_object_store_error<S>(
120 kind: &ErrorKind,
121 message: &String,
122 serializer: S,
123) -> Result<S::Ok, S::Error>
124where
125 S: Serializer,
126{
127 let mut ser = serializer.serialize_struct("ObjectStoreError", 2)?;
128 ser.serialize_field("kind", &kind.to_string())?;
129 ser.serialize_field("message", message)?;
130 ser.end()
131}
132
133impl StorageError {
134 pub fn stdio(kind: ErrorKind, operation: &'static str, path: impl ToString) -> Self {
135 Self::StdIo {
136 kind,
137 operation,
138 path: Some(path.to_string()),
139 }
140 }
141
142 pub fn kind(&self) -> ErrorKind {
143 match self {
144 StorageError::StdIo { kind, .. } => *kind,
145 StorageError::StorageLocked(..) => ErrorKind::ResourceBusy,
146 StorageError::NoPersistentId(_) => ErrorKind::Other,
147 StorageError::CheckpointNotFound(_) => ErrorKind::NotFound,
148 StorageError::StorageDisabled => ErrorKind::Other,
149 StorageError::BloomFilter => ErrorKind::Other,
150 StorageError::InvalidPath(_) => ErrorKind::Other,
151 StorageError::InvalidURL(_) => ErrorKind::Other,
152 StorageError::ObjectStore { kind, .. } => *kind,
153 StorageError::BackendNotSupported(_) => ErrorKind::Other,
154 StorageError::InvalidBackendConfig { .. } => ErrorKind::Other,
155 StorageError::JsonError(_) => ErrorKind::Other,
156 }
157 }
158
159 pub fn ignore_notfound<T>(result: Result<T, Self>) -> Result<(), Self> {
160 match result {
161 Ok(_) => Ok(()),
162 Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
163 Err(error) => Err(error),
164 }
165 }
166}