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)]
12pub enum StorageError {
13 #[error("{0}")]
15 StdIo(ErrorKind),
16
17 #[error("A process with PID {0} is already using the storage directory {1:?}.")]
22 StorageLocked(u32, PathBuf),
23
24 #[error("Couldn't find the specified checkpoint ({0:?}).")]
26 CheckpointNotFound(Uuid),
27
28 #[error("Cannot perform operation because storage is not enabled.")]
30 StorageDisabled,
31 #[error("Failed to serialize/deserialize bloom filter.")]
33 BloomFilter,
34
35 #[error("Path is not valid in storage: {}", .0.display())]
40 InvalidPath(PathBuf),
41
42 #[error("Unable to parse URL {0:?}")]
44 InvalidURL(String),
45
46 #[error("Error accessing object store: {message}")]
48 ObjectStore { kind: ErrorKind, message: String },
49
50 #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
52 )]
53 BackendNotSupported(StorageBackendConfig),
54}
55
56impl From<std::io::Error> for StorageError {
57 fn from(value: std::io::Error) -> Self {
58 Self::StdIo(value.kind())
59 }
60}
61
62impl From<ObjectStoreError> for StorageError {
63 fn from(value: ObjectStoreError) -> Self {
64 let kind = match value {
65 ObjectStoreError::NotFound { .. } => ErrorKind::NotFound,
66 ObjectStoreError::NotSupported { .. } => ErrorKind::Unsupported,
67 ObjectStoreError::AlreadyExists { .. } => ErrorKind::AlreadyExists,
68 ObjectStoreError::NotImplemented => ErrorKind::Unsupported,
69 ObjectStoreError::PermissionDenied { .. }
70 | ObjectStoreError::Unauthenticated { .. } => ErrorKind::PermissionDenied,
71 ObjectStoreError::InvalidPath { .. } => {
72 ErrorKind::Other
74 }
75 ObjectStoreError::Generic { .. }
76 | ObjectStoreError::JoinError { .. }
77 | ObjectStoreError::Precondition { .. }
78 | ObjectStoreError::NotModified { .. }
79 | ObjectStoreError::UnknownConfigurationKey { .. }
80 | _ => ErrorKind::Other,
81 };
82 Self::ObjectStore {
83 kind,
84 message: value.to_string(),
85 }
86 }
87}
88
89impl Serialize for StorageError {
90 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
91 where
92 S: Serializer,
93 {
94 match self {
95 Self::StdIo(error) => {
96 let mut ser = serializer.serialize_struct("IOError", 1)?;
97 ser.serialize_field("kind", &error.to_string())?;
98 ser.end()
99 }
100 error => error.serialize(serializer),
101 }
102 }
103}
104
105impl StorageError {
106 pub fn kind(&self) -> ErrorKind {
107 match self {
108 StorageError::StdIo(kind) => *kind,
109 StorageError::StorageLocked(..) => ErrorKind::ResourceBusy,
110 StorageError::CheckpointNotFound(_) => ErrorKind::NotFound,
111 StorageError::StorageDisabled => ErrorKind::Other,
112 StorageError::BloomFilter => ErrorKind::Other,
113 StorageError::InvalidPath(_) => ErrorKind::Other,
114 StorageError::InvalidURL(_) => ErrorKind::Other,
115 StorageError::ObjectStore { kind, .. } => *kind,
116 StorageError::BackendNotSupported(_) => ErrorKind::Other,
117 }
118 }
119
120 pub fn ignore_notfound<T>(result: Result<T, Self>) -> Result<(), Self> {
121 match result {
122 Ok(_) => Ok(()),
123 Err(error) if error.kind() == ErrorKind::NotFound => Ok(()),
124 Err(error) => Err(error),
125 }
126 }
127}