error-chain icon indicating copy to clipboard operation
error-chain copied to clipboard

Backtrace/cause tunneling through trait objects

Open sfackler opened this issue 8 years ago • 2 comments
trafficstars

You sometimes need a layer of Box<Error> in an API when e.g. making a pluginizable framework. You can currently propagate the cause chain up properly but not a backtrace.

It would be nice if ChainedError were split with a top-level trait which just extends Error and has extract_backtrace and error_chain! were updated to allow propagation of the backtrace through it. Something like

error_chain! {
    errors {
        Plugin(e: Box<BaseChainedError>) {
            cause(e)
            backtrace(e)
            description("error from a plugin")
            display("error from a plugin: {}", e)
        }
    }
}

sfackler avatar Jan 04 '17 17:01 sfackler

Not sure I understand, what is BaseChainedError?

Yamakaky avatar Jan 04 '17 21:01 Yamakaky

pub trait BaseChainedError: Error + 'static + Sync + Send {
    fn iter(&self) -> ErrorChainIter;
    fn backtrace(&self) -> Option<&Backtrace>;
}

pub trait ChainedError: BaseChainedError {
    type ErrorKind;
    fn from_kind(kind: Self::ErrorKind) -> Self where Self: Sized;
    fn kind(&self) -> &Self::ErrorKind;
}

We can't just use ChainedError since it has the associated ErrorKind type.

sfackler avatar Jan 04 '17 21:01 sfackler