gix_error/concrete/
chain.rs

1use crate::write_location;
2use std::fmt::{Debug, Display, Formatter};
3use std::panic::Location;
4
5/// A generic error which represents a linked-list of errors and exposes it with [source()](std::error::Error::source).
6/// It's meant to be the target of a conversion of any [Exn](crate::Exn) error tree.
7///
8/// It's useful for inter-op with other error handling crates like `anyhow` which offer simplified access to the error chain,
9/// and thus is expected to be wrapped in one of their types intead of being used directly.
10pub struct ChainedError {
11    pub(crate) err: Box<dyn std::error::Error + Send + Sync + 'static>,
12    pub(crate) location: &'static Location<'static>,
13    pub(crate) source: Option<Box<ChainedError>>,
14}
15
16impl Debug for ChainedError {
17    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
18        Debug::fmt(&self.err, f)
19    }
20}
21
22impl Display for ChainedError {
23    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
24        Display::fmt(&self.err, f)?;
25        if !f.alternate() {
26            write_location(f, self.location)?;
27        }
28        Ok(())
29    }
30}
31
32impl std::error::Error for ChainedError {
33    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
34        self.source.as_ref().map(|e| e as &(dyn std::error::Error + 'static))
35    }
36}