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