feldera_storage/
error.rs

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/// An error that can occur when using the storage backend.
11#[derive(Clone, Error, Debug)]
12pub enum StorageError {
13    /// I/O error.
14    #[error("{0}")]
15    StdIo(ErrorKind),
16
17    /// A process already locked the provided storage directory.
18    ///
19    /// If this is not expected, please remove the lock file manually, after verifying
20    /// that the process with the given PID no longer exists.
21    #[error("A process with PID {0} is already using the storage directory {1:?}.")]
22    StorageLocked(u32, PathBuf),
23
24    /// Unknown checkpoint specified in configuration.
25    #[error("Couldn't find the specified checkpoint ({0:?}).")]
26    CheckpointNotFound(Uuid),
27
28    /// Cannot perform operation because storage is not enabled.
29    #[error("Cannot perform operation because storage is not enabled.")]
30    StorageDisabled,
31    /// Error while creating a bloom filter.
32    #[error("Failed to serialize/deserialize bloom filter.")]
33    BloomFilter,
34
35    /// Path is not UTF-8.
36    #[error("Path is not UTF-8: {}", .0.display())]
37    InvalidPath(PathBuf),
38
39    /// Unable to parse URL.
40    #[error("Unable to parse URL {0:?}")]
41    InvalidURL(String),
42
43    /// Error accessing object store.
44    #[error("Error accessing object store: {0}")]
45    ObjectStore(String),
46
47    /// The requested storage backend is not available.
48    #[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
49    )]
50    BackendNotSupported(StorageBackendConfig),
51}
52
53impl From<std::io::Error> for StorageError {
54    fn from(value: std::io::Error) -> Self {
55        Self::StdIo(value.kind())
56    }
57}
58
59impl From<ObjectStoreError> for StorageError {
60    fn from(value: ObjectStoreError) -> Self {
61        Self::ObjectStore(value.to_string())
62    }
63}
64
65impl Serialize for StorageError {
66    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
67    where
68        S: Serializer,
69    {
70        match self {
71            Self::StdIo(error) => {
72                let mut ser = serializer.serialize_struct("IOError", 1)?;
73                ser.serialize_field("kind", &error.to_string())?;
74                ser.end()
75            }
76            error => error.serialize(serializer),
77        }
78    }
79}
80
81impl StorageError {
82    /// Returns true if this error likely indicates "file (or object) not
83    /// found".
84    pub fn is_not_found(&self) -> bool {
85        match self {
86            Self::StdIo(kind) if *kind == ErrorKind::NotFound => true,
87            Self::ObjectStore(_) => {
88                // XXX This is imprecise, because we can't easily look at the
89                // details to find out whether it's "object not found". It would
90                // be good to improve it.
91                true
92            }
93            _ => false,
94        }
95    }
96}