2023-07-30 17:48:55 +02:00
|
|
|
use std::{error::Error, fmt::Display};
|
|
|
|
|
|
|
|
use tonic::{Status, Code};
|
2023-07-01 13:54:34 +02:00
|
|
|
use tracing::warn;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
impl<T, E> IgnorableError for Result<T, E>
|
|
|
|
where E : std::fmt::Display {
|
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(_) => {},
|
|
|
|
Err(e) => warn!("{}: {}", msg, e),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-30 17:48:55 +02:00
|
|
|
|
|
|
|
// TODO split this into specific errors for various parts of the library
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum CodempError {
|
|
|
|
Transport {
|
|
|
|
status: Code,
|
|
|
|
message: String,
|
|
|
|
},
|
|
|
|
Channel { },
|
|
|
|
|
|
|
|
// TODO filler error, remove later
|
|
|
|
Filler {
|
|
|
|
message: String,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for CodempError {}
|
|
|
|
|
|
|
|
impl Display for CodempError {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
|
|
|
Self::Transport { status, message } => write!(f, "Transport error: ({}) {}", status, message),
|
|
|
|
Self::Channel { } => write!(f, "Channel error"),
|
|
|
|
_ => write!(f, "Unknown error"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Status> for CodempError {
|
|
|
|
fn from(status: Status) -> Self {
|
|
|
|
CodempError::Transport { status: status.code(), message: status.message().to_string() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<tonic::transport::Error> for CodempError {
|
|
|
|
fn from(err: tonic::transport::Error) -> Self {
|
|
|
|
CodempError::Transport {
|
|
|
|
status: Code::Unknown, message: format!("underlying transport error: {:?}", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|