use std::error::Error;
use actix_web::{http::StatusCode, ResponseError};
use thiserror::Error;
#[derive(Debug, Error)]
pub enum StorageError {
#[error("StorageError: Method not supported for the storage backend provided")]
MethodNotSupported,
#[error("StorageError: Serialization failed")]
SerializationError,
#[error("StorageError: Deserialization failed")]
DeserializationError,
#[error("StorageError: {:?}", self)]
Custom(Box<dyn Error + Send>),
}
impl StorageError {
pub fn custom<E>(err: E) -> Self
where
E: 'static + Error + Send,
{
Self::Custom(Box::new(err))
}
}
impl ResponseError for StorageError {
fn status_code(&self) -> StatusCode {
StatusCode::INTERNAL_SERVER_ERROR
}
}
pub type Result<T> = std::result::Result<T, StorageError>;
#[cfg(test)]
mod test {
use super::*;
use thiserror::Error;
#[derive(Debug, Error)]
#[error("My error")]
struct MyError;
#[test]
fn test_error() {
let e = MyError;
let s = StorageError::custom(e);
assert!(s.status_code() == StatusCode::INTERNAL_SERVER_ERROR);
if let StorageError::Custom(err) = s {
assert!(format!("{}", err) == "My error");
}
}
}