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("{0}")]
15 #[serde(serialize_with = "serialize_io_error")]
16 StdIo(ErrorKind),
17
18 #[error("A process with PID {0} is already using the storage directory {1:?}.")]
23 StorageLocked(u32, PathBuf),
24
25 #[error("Couldn't find the specified checkpoint ({0:?}).")]
27 CheckpointNotFound(Uuid),
28
29 #[error("Internal error: operator {0} has not been assigned a persistent id.")]
31 NoPersistentId(String),
32
33 #[error("Cannot perform operation because storage is not enabled.")]
35 StorageDisabled,
36 #[error("Failed to serialize/deserialize bloom filter.")]
38 BloomFilter,
39
40 #[error("Path is not valid in storage: {}", .0.display())]
45 InvalidPath(PathBuf),
46
47 #[error("Unable to parse URL {0:?}")]
49 InvalidURL(String),
50
51 #[error("Error accessing object store: {message}")]
53 #[serde(serialize_with = "serialize_object_store_error")]
54 ObjectStore { kind: ErrorKind, message: String },
55
56 #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
58 )]
59 BackendNotSupported(Box<StorageBackendConfig>),
60
61 #[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 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}