2023-08-20 00:46:55 +02:00
|
|
|
//! ### Errors
|
|
|
|
//!
|
|
|
|
//! library error helpers and types
|
|
|
|
|
2023-08-19 21:44:27 +02:00
|
|
|
use std::{result::Result as StdResult, error::Error as StdError, fmt::Display};
|
2023-07-30 17:48:55 +02:00
|
|
|
|
2023-07-01 13:54:34 +02:00
|
|
|
use tracing::warn;
|
|
|
|
|
2023-08-20 00:46:55 +02:00
|
|
|
/// an error which can be ignored with just a warning entry
|
2023-07-01 13:54:34 +02:00
|
|
|
pub trait IgnorableError {
|
2023-07-01 14:01:08 +02:00
|
|
|
fn unwrap_or_warn(self, msg: &str);
|
2023-07-01 13:54:34 +02:00
|
|
|
}
|
|
|
|
|
2023-08-19 21:44:27 +02:00
|
|
|
impl<T, E> IgnorableError for StdResult<T, E>
|
2023-11-24 10:43:37 +01:00
|
|
|
where E : std::fmt::Debug {
|
2023-07-01 14:01:08 +02:00
|
|
|
fn unwrap_or_warn(self, msg: &str) {
|
2023-07-01 13:54:34 +02:00
|
|
|
match self {
|
|
|
|
Ok(_) => {},
|
2023-11-24 10:43:37 +01:00
|
|
|
Err(e) => warn!("{}: {:?}", msg, e),
|
2023-07-01 13:54:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 17:48:55 +02:00
|
|
|
|
2023-09-04 03:08:52 +02:00
|
|
|
|
|
|
|
/// an error which can be ignored with just a warning entry and returning the default value
|
|
|
|
pub trait IgnorableDefaultableError<T> {
|
|
|
|
fn unwrap_or_warn_default(self, msg: &str) -> T;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> IgnorableDefaultableError<T> for StdResult<T, E>
|
|
|
|
where E : std::fmt::Display, T: Default {
|
|
|
|
fn unwrap_or_warn_default(self, msg: &str) -> T {
|
|
|
|
match self {
|
|
|
|
Ok(x) => x,
|
|
|
|
Err(e) => {
|
|
|
|
warn!("{}: {}", msg, e);
|
|
|
|
T::default()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-20 00:46:55 +02:00
|
|
|
/// result type for codemp errors
|
2023-08-19 21:44:27 +02:00
|
|
|
pub type Result<T> = StdResult<T, Error>;
|
|
|
|
|
2023-07-30 17:48:55 +02:00
|
|
|
// TODO split this into specific errors for various parts of the library
|
2023-08-20 00:46:55 +02:00
|
|
|
/// codemp error type for library issues
|
2023-07-30 17:48:55 +02:00
|
|
|
#[derive(Debug)]
|
2023-08-17 02:58:55 +02:00
|
|
|
pub enum Error {
|
2023-08-20 00:46:55 +02:00
|
|
|
/// errors caused by tonic http layer
|
2023-07-30 17:48:55 +02:00
|
|
|
Transport {
|
2023-09-10 03:40:31 +02:00
|
|
|
status: String,
|
2023-07-30 17:48:55 +02:00
|
|
|
message: String,
|
|
|
|
},
|
2023-08-20 00:46:55 +02:00
|
|
|
/// errors caused by async channels
|
2023-08-11 15:34:04 +02:00
|
|
|
Channel {
|
|
|
|
send: bool
|
|
|
|
},
|
2023-08-20 00:46:55 +02:00
|
|
|
/// errors caused by wrong usage of library objects
|
2023-08-16 23:09:47 +02:00
|
|
|
InvalidState {
|
|
|
|
msg: String,
|
|
|
|
},
|
2023-07-30 17:48:55 +02:00
|
|
|
|
2023-11-17 05:47:57 +01:00
|
|
|
/// errors caused by wrong interlocking, safe to retry
|
|
|
|
Deadlocked,
|
|
|
|
|
2023-08-20 00:46:55 +02:00
|
|
|
/// if you see these errors someone is being lazy (:
|
|
|
|
Filler { // TODO filler error, remove later
|
2023-07-30 17:48:55 +02:00
|
|
|
message: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-08-17 02:58:55 +02:00
|
|
|
impl StdError for Error {}
|
2023-07-30 17:48:55 +02:00
|
|
|
|
2023-08-17 02:58:55 +02:00
|
|
|
impl Display for Error {
|
2023-07-30 17:48:55 +02:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Transport { status, message } => write!(f, "Transport error: ({}) {}", status, message),
|
2023-08-11 15:34:04 +02:00
|
|
|
Self::Channel { send } => write!(f, "Channel error (send:{})", send),
|
2023-07-30 17:48:55 +02:00
|
|
|
_ => write!(f, "Unknown error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 03:40:31 +02:00
|
|
|
impl From<tonic::Status> for Error {
|
|
|
|
fn from(status: tonic::Status) -> Self {
|
|
|
|
Error::Transport {
|
|
|
|
status: status.code().to_string(),
|
|
|
|
message: status.message().to_string()
|
|
|
|
}
|
2023-07-30 17:48:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 02:58:55 +02:00
|
|
|
impl From<tonic::transport::Error> for Error {
|
2023-07-30 17:48:55 +02:00
|
|
|
fn from(err: tonic::transport::Error) -> Self {
|
2023-08-17 02:58:55 +02:00
|
|
|
Error::Transport {
|
2023-09-10 03:40:31 +02:00
|
|
|
status: tonic::Code::Unknown.to_string(),
|
|
|
|
message: format!("underlying transport error: {:?}", err)
|
2023-07-30 17:48:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-08-11 15:34:04 +02:00
|
|
|
|
2023-09-10 03:40:31 +02:00
|
|
|
impl<T> From<tokio::sync::mpsc::error::SendError<T>> for Error {
|
|
|
|
fn from(_value: tokio::sync::mpsc::error::SendError<T>) -> Self {
|
2023-08-17 02:58:55 +02:00
|
|
|
Error::Channel { send: true }
|
2023-08-11 15:34:04 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-16 18:58:42 +02:00
|
|
|
|
2024-01-25 02:13:45 +01:00
|
|
|
impl<T> From<tokio::sync::watch::error::SendError<T>> for Error {
|
|
|
|
fn from(_value: tokio::sync::watch::error::SendError<T>) -> Self {
|
|
|
|
Error::Channel { send: true }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-10 03:40:31 +02:00
|
|
|
impl From<tokio::sync::broadcast::error::RecvError> for Error {
|
|
|
|
fn from(_value: tokio::sync::broadcast::error::RecvError) -> Self {
|
2023-08-17 02:58:55 +02:00
|
|
|
Error::Channel { send: false }
|
2023-08-16 18:58:42 +02:00
|
|
|
}
|
|
|
|
}
|
2023-08-21 02:35:56 +02:00
|
|
|
|
2023-09-10 03:40:31 +02:00
|
|
|
impl From<tokio::sync::watch::error::RecvError> for Error {
|
|
|
|
fn from(_value: tokio::sync::watch::error::RecvError) -> Self {
|
2023-08-21 02:35:56 +02:00
|
|
|
Error::Channel { send: false }
|
|
|
|
}
|
|
|
|
}
|