use std::any::Any;
use std::sync::Arc;
#[derive(Debug, Clone)]
pub struct RelayError {
pub msg_id: u64,
pub error: Arc<dyn std::error::Error + Send + Sync>,
pub source: &'static str,
}
impl RelayError {
pub fn new<E: std::error::Error + Send + Sync + 'static>(
msg_id: u64,
error: E,
source: &'static str,
) -> Self {
Self {
msg_id,
error: Arc::new(error),
source,
}
}
}
impl std::fmt::Display for RelayError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "[{}] msg {}: {}", self.source, self.msg_id, self.error)
}
}
impl std::error::Error for RelayError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(self.error.as_ref())
}
}
#[derive(Debug, Clone)]
pub struct PanicError {
message: String,
}
impl PanicError {
pub fn new(payload: Box<dyn Any + Send>) -> Self {
let message = if let Some(s) = payload.downcast_ref::<&str>() {
s.to_string()
} else if let Some(s) = payload.downcast_ref::<String>() {
s.clone()
} else {
"panic occurred (non-string payload)".to_string()
};
Self { message }
}
}
impl std::fmt::Display for PanicError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "panic: {}", self.message)
}
}
impl std::error::Error for PanicError {}