use std::fmt;
#[derive(Debug)]
pub struct ProgramError(Box<dyn std::error::Error + Send + Sync>);
impl ProgramError {
pub fn new<E>(err: E) -> Self
where
E: std::error::Error + Send + Sync + 'static,
{
Self(Box::new(err))
}
pub fn msg(msg: impl Into<String>) -> Self {
Self(msg.into().into())
}
}
impl std::error::Error for ProgramError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.0.source()
}
}
impl fmt::Display for ProgramError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "rpc program error: {}", self.0)
}
}
impl From<std::io::Error> for ProgramError {
fn from(err: std::io::Error) -> Self {
Self::new(err)
}
}
impl From<Box<dyn std::error::Error + Send + Sync>> for ProgramError {
fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
Self(err)
}
}