cosmwasm_vm/errors/
backtrace.rs

1use core::fmt::{Debug, Display, Formatter, Result};
2use std::backtrace::Backtrace;
3
4/// This wraps an actual backtrace to allow us to use this in conjunction with [`thiserror::Error`]
5pub struct BT(Box<Backtrace>);
6
7impl BT {
8    #[track_caller]
9    pub fn capture() -> Self {
10        BT(Box::new(Backtrace::capture()))
11    }
12}
13
14impl Debug for BT {
15    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
16        Debug::fmt(&self.0, f)
17    }
18}
19
20impl Display for BT {
21    fn fmt(&self, f: &mut Formatter<'_>) -> Result {
22        Display::fmt(&self.0, f)
23    }
24}
25
26/// This macro implements `From` for a given error type to a given error type where
27/// the target error has a `backtrace` field.
28/// This is meant as a replacement for `thiserror`'s `#[from]` attribute, which does not
29/// work with our custom backtrace wrapper.
30macro_rules! impl_from_err {
31    ($from:ty, $to:ty, $map:path) => {
32        impl From<$from> for $to {
33            fn from(err: $from) -> Self {
34                $map {
35                    source: err,
36                    backtrace: $crate::errors::backtrace::BT::capture(),
37                }
38            }
39        }
40    };
41}
42pub(crate) use impl_from_err;