Geoffrey-rs/geoffrey_db/src/error.rs

48 lines
1.5 KiB
Rust

use geoffrey_models::models::response::api_error::GeoffreyAPIError;
pub type Result<T> = std::result::Result<T, GeoffreyDBError>;
#[derive(Debug)]
pub enum GeoffreyDBError {
SledError(sled::Error),
SerdeJsonError(serde_json::Error),
NotUnique,
NotFound,
}
impl std::error::Error for GeoffreyDBError {}
impl std::fmt::Display for GeoffreyDBError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GeoffreyDBError::SledError(e) => write!(f, "Sled Error: {}", e),
GeoffreyDBError::SerdeJsonError(e) => write!(f, "Serde JSON Error: {}", e),
GeoffreyDBError::NotUnique => write!(f, "Entry is not unique."),
GeoffreyDBError::NotFound => write!(f, "Entry was not found."),
}
}
}
impl From<sled::Error> for GeoffreyDBError {
fn from(e: sled::Error) -> Self {
GeoffreyDBError::SledError(e)
}
}
impl From<serde_json::Error> for GeoffreyDBError {
fn from(e: serde_json::Error) -> Self {
GeoffreyDBError::SerdeJsonError(e)
}
}
impl From<GeoffreyDBError> for GeoffreyAPIError {
fn from(e: GeoffreyDBError) -> Self {
match e {
GeoffreyDBError::SledError(_) => GeoffreyAPIError::DatabaseError(e.to_string()),
GeoffreyDBError::SerdeJsonError(_) => GeoffreyAPIError::DatabaseError(e.to_string()),
GeoffreyDBError::NotUnique => GeoffreyAPIError::EntryNotUnique,
GeoffreyDBError::NotFound => GeoffreyAPIError::EntryNotFound,
}
}
}