2023-02-12 11:39:31 +01:00
|
|
|
use thiserror::Error;
|
2023-03-25 22:17:23 +01:00
|
|
|
use tokio::task::JoinError;
|
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.
|
2024-03-23 21:49:49 +01:00
|
|
|
#[derive(Debug, Error, PartialEq)]
|
2023-02-12 11:39:31 +01:00
|
|
|
pub enum Error {
|
|
|
|
#[error("invalid request: {0}")]
|
|
|
|
InvalidRequest(String),
|
|
|
|
|
|
|
|
#[error("internal storage error: {0}")]
|
|
|
|
StorageError(String),
|
|
|
|
}
|
2023-03-03 22:32:07 +01:00
|
|
|
|
2023-03-25 22:17:23 +01:00
|
|
|
impl From<JoinError> for Error {
|
|
|
|
fn from(value: JoinError) -> 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
|
|
|
|
2023-11-13 13:32:24 +01:00
|
|
|
impl From<crate::tonic::Error> for Error {
|
|
|
|
fn from(value: crate::tonic::Error) -> Self {
|
2023-10-12 19:26:52 +02:00
|
|
|
Self::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-21 00:36:19 +02:00
|
|
|
impl From<redb::Error> for Error {
|
|
|
|
fn from(value: redb::Error) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::DatabaseError> for Error {
|
|
|
|
fn from(value: redb::DatabaseError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::TableError> for Error {
|
|
|
|
fn from(value: redb::TableError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::TransactionError> for Error {
|
|
|
|
fn from(value: redb::TransactionError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::StorageError> for Error {
|
|
|
|
fn from(value: redb::StorageError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<redb::CommitError> for Error {
|
|
|
|
fn from(value: redb::CommitError) -> Self {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-18 17:30:55 +01:00
|
|
|
impl From<std::io::Error> for Error {
|
|
|
|
fn from(value: std::io::Error) -> Self {
|
|
|
|
if value.kind() == std::io::ErrorKind::InvalidInput {
|
|
|
|
Error::InvalidRequest(value.to_string())
|
|
|
|
} else {
|
|
|
|
Error::StorageError(value.to_string())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|