Something like:
#[derive(Debug)]
pub struct CuError {
message: String,
cause: Option<Box<dyn std::error::Error + Send + Sync>>,
}
impl CuError {
pub fn new(message: impl Into<String>) -> Self {
Self { message: message.into(), cause: None }
}
pub fn with_cause<E>(message: impl Into<String>, cause: E) -> Self
where
E: Into<Box<dyn std::error::Error + Send + Sync>>,
{
Self { message: message.into(), cause: Some(cause.into()) }
}
}
// plus a free function ...
pub fn with_cause(msg: impl Into<String>, err: impl Into<Box<dyn Error + Send + Sync>>) -> CuError {
CuError::with_cause(msg, err)
}
and just inline:
use ...::with_cause;
return Err(with_cause("tada", &error));