use feldera_types::config::StorageBackendConfig;
use object_store::Error as ObjectStoreError;
use serde::ser::SerializeStruct;
use serde::{Serialize, Serializer};
use std::io::ErrorKind;
use std::path::PathBuf;
use thiserror::Error;
use uuid::Uuid;
#[derive(Clone, Error, Debug)]
pub enum StorageError {
#[error("{0}")]
StdIo(ErrorKind),
#[error("A process with PID {0} is already using the storage directory {1:?}.")]
StorageLocked(u32, PathBuf),
#[error("Couldn't find the specified checkpoint ({0:?}).")]
CheckpointNotFound(Uuid),
#[error("Cannot perform operation because storage is not enabled.")]
StorageDisabled,
#[error("Failed to serialize/deserialize bloom filter.")]
BloomFilter,
#[error("Path is not UTF-8: {}", .0.display())]
InvalidPath(PathBuf),
#[error("Unable to parse URL {0:?}")]
InvalidURL(String),
#[error("Error accessing object store: {0}")]
ObjectStore(String),
#[error("The requested storage backend ({0:?}) is not available in the open-source version of feldera"
)]
BackendNotSupported(StorageBackendConfig),
}
impl From<std::io::Error> for StorageError {
fn from(value: std::io::Error) -> Self {
Self::StdIo(value.kind())
}
}
impl From<ObjectStoreError> for StorageError {
fn from(value: ObjectStoreError) -> Self {
Self::ObjectStore(value.to_string())
}
}
impl Serialize for StorageError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
Self::StdIo(error) => {
let mut ser = serializer.serialize_struct("IOError", 1)?;
ser.serialize_field("kind", &error.to_string())?;
ser.end()
}
error => error.serialize(serializer),
}
}
}