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: Box<StorageBackendConfig>,
66 },
67
68 #[error("Error deserializing JSON: {0}")]
69 JsonError(String),
70}
71
72impl From<std::io::Error> for StorageError {
73 fn from(value: std::io::Error) -> Self {
74 Self::StdIo(value.kind())
75 }
76}
77
78impl From<ObjectStoreError> for StorageError {
79 fn from(value: ObjectStoreError) -> Self {
80 let kind = match value {
81 ObjectStoreError::NotFound { .. } => ErrorKind::NotFound,
82 ObjectStoreError::NotSupported { .. } => ErrorKind::Unsupported,
83 ObjectStoreError::AlreadyExists { .. } => ErrorKind::AlreadyExists,
84 ObjectStoreError::NotImplemented => ErrorKind::Unsupported,
85 ObjectStoreError::PermissionDenied { .. }
86 | ObjectStoreError::Unauthenticated { .. } => ErrorKind::PermissionDenied,
87 ObjectStoreError::InvalidPath { .. } => {
88 ErrorKind::Other
90 }
91 ObjectStoreError::Generic { .. }
92 | ObjectStoreError::JoinError { .. }
93 | ObjectStoreError::Precondition { .. }
94 | ObjectStoreError::NotModified { .. }
95 | ObjectStoreError::UnknownConfigurationKey { .. }
96 | _ => ErrorKind::Other,
97 };
98 Self::ObjectStore {
99 kind,
100 message: value.to_string(),
101 }
102 }
103}
104
105fn serialize_io_error<S>(kind: &ErrorKind, serializer: S) -> Result<S::Ok, S::Error>
106where
107 S: Serializer,
108{
109 let mut ser = serializer.serialize_struct("IOError", 1)?;
110 ser.serialize_field("kind", &kind.to_string())?;
111 ser.end()
112}
113
114fn serialize_object_store_error<S>(
115 kind: &ErrorKind,
116 message: &String,
117 serializer: S,
118) -> Result<S::Ok, S::Error>
119where
120 S: Serializer,
121{
122 let mut ser = serializer.serialize_struct("ObjectStoreError", 2)?;
123 ser.serialize_field("kind", &kind.to_string())?;
124 ser.serialize_field("message", message)?;
125 ser.end()
126}
127
128impl StorageError {
129 pub fn kind(&self) -> ErrorKind {
130 match self {
131 StorageError::StdIo(kind) => *kind,
132 StorageError::StorageLocked(..) => ErrorKind::ResourceBusy,
133 StorageError::NoPersistentId(_) => ErrorKind::Other,
134 StorageError::CheckpointNotFound(_) => ErrorKind::NotFound,
135 StorageError::StorageDisabled => ErrorKind::Other,
136 StorageError::BloomFilter => ErrorKind::Other,
137 StorageError::InvalidPath(_) => ErrorKind::Other,
138 StorageError::InvalidURL(_) => ErrorKind::Other,
139 StorageError::ObjectStore { kind, .. } => *kind,
140 StorageError::BackendNotSupported(_) => ErrorKind::Other,
141 StorageError::InvalidBackendConfig { .. } => ErrorKind::Other,
142 StorageError::JsonError(_) => ErrorKind::Other,
143 }
144 }
145
146 pub fn ignore_notfound<T>(result: Result<T, Self>) -> Result<(), Self> {
147 match result {
148 Ok(_) => Ok(()),
149 Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
150 Err(error) => Err(error),
151 }
152 }
153}