2023-03-03 22:32:07 +01:00
|
|
|
use std::sync::PoisonError;
|
2023-02-12 11:39:31 +01:00
|
|
|
use thiserror::Error;
|
2023-02-12 12:34:15 +01:00
|
|
|
use tonic::Status;
|
2023-02-12 11:39:31 +01:00
|
|
|
|
|
|
|
/// Errors related to communication with the store.
|
|
|
|
#[derive(Debug, Error)]
|
|
|
|
pub enum Error {
|
|
|
|
#[error("invalid request: {0}")]
|
|
|
|
InvalidRequest(String),
|
|
|
|
|
|
|
|
#[error("internal storage error: {0}")]
|
|
|
|
StorageError(String),
|
|
|
|
}
|
2023-03-03 22:32:07 +01:00
|
|
|
|
|
|
|
impl<T> From<PoisonError<T>> for Error {
|
|
|
|
fn from(value: PoisonError<T>) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
2023-02-12 12:34:15 +01:00
|
|
|
|
|
|
|
impl From<Error> for Status {
|
|
|
|
fn from(value: Error) -> Self {
|
|
|
|
match value {
|
|
|
|
Error::InvalidRequest(msg) => Status::invalid_argument(msg),
|
|
|
|
Error::StorageError(msg) => Status::data_loss(format!("storage error: {}", msg)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 22:31:50 +01:00
|
|
|
|
|
|
|
// TODO: this should probably go somewhere else?
|
|
|
|
impl From<Error> for std::io::Error {
|
|
|
|
fn from(value: Error) -> Self {
|
|
|
|
match value {
|
|
|
|
Error::InvalidRequest(msg) => Self::new(std::io::ErrorKind::InvalidInput, msg),
|
|
|
|
Error::StorageError(msg) => Self::new(std::io::ErrorKind::Other, msg),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|