use std::backtrace::{Backtrace, BacktraceStatus};
use std::convert::Infallible;
use std::fmt::{self, Display, Formatter};
use std::num::ParseIntError;
use std::ops::Deref;
use std::str::Utf8Error;
#[macro_export]
macro_rules! err {
(with: $error:expr, $($arg:tt)*) => {{
let error = format!($($arg)*);
let source = Box::new($error);
$crate::error::Error::new(error, Some(source))
}};
($($arg:tt)*) => {{
let error = format!($($arg)*);
$crate::error::Error::new(error, None)
}};
}
#[derive(Debug)]
pub struct Error {
pub error: String,
pub source: Option<Box<dyn std::error::Error + Send>>,
pub backtrace: Backtrace,
}
impl Error {
#[doc(hidden)]
pub fn new(error: String, source: Option<Box<dyn std::error::Error + Send>>) -> Self {
let backtrace = Backtrace::capture();
Self { error, source, backtrace }
}
pub fn has_backtrace(&self) -> bool {
self.backtrace.status() == BacktraceStatus::Captured
}
}
impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
writeln!(f, "{}", self.error)?;
if let Some(source) = &self.source {
writeln!(f, " caused by: {source}")?;
}
Ok(())
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
let boxed = self.source.as_ref()?;
Some(boxed.deref())
}
}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
err!(with: value, "An I/O error occurred")
}
}
impl From<Utf8Error> for Error {
fn from(value: Utf8Error) -> Self {
err!(with: value, "Value is not valid UTF-8")
}
}
impl From<ParseIntError> for Error {
fn from(value: ParseIntError) -> Self {
err!(with: value, "Value is not a valid integer")
}
}
impl From<Infallible> for Error {
fn from(_: Infallible) -> Self {
unreachable!("infallible variant can never be constructed")
}
}