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

Using error-chain with boxed errors

Open dgrnbrg opened this issue 8 years ago • 4 comments
trafficstars

Hello, I have an API which returns a Result<(),Box<std::error::Error + Sync + Send + 'static>>, and I'd like to chain on that. However, I get this error:

error: no method named `chain_err` found for type `std::result::Result<(), std::boxed::Box<std::error::Error + std::marker::Send + std::marker::Sync + 'static>>` in the current scope
   --> src/engine.rs:408:30
    |
408 |                             .chain_err(|| ErrorKind::BoxedIntegrationError)?
    |                              ^^^^^^^^^
    |
    = note: the method `chain_err` exists but the following trait bounds were not satisfied:
            `std::boxed::Box<std::error::Error + std::marker::Send + std::marker::Sync> : std::error::Error`
    = help: items from traits can only be used if the trait is implemented and in scope; the following trait defines an item `chain_err`, perhaps you need to implement it:
    = help: candidate #1: `errors::ResultExt`

Any ideas on how to approach this? The API is 3rd party, so I can't modify it to return a specific error--it must be the boxed trait.

dgrnbrg avatar Jun 01 '17 23:06 dgrnbrg

Sorry for the delay. Could you give more code?

Yamakaky avatar Jun 03 '17 18:06 Yamakaky

Here's a minimal case. It should be possible to chain with results that contain boxed errors, but it isn't. Note that demo fails due to the Err variant being boxed, but of course other works fine.

#[macro_use]
extern crate error_chain;

pub mod errors {
    error_chain! {
    }
}

use errors::*;

fn demo(input: std::result::Result<(),Box<std::error::Error + Sync + Send + 'static>>) -> Result<()> {
    input.chain_err(|| "fails")
}

fn other(input: Result<()>) -> Result<()> {
    input.chain_err(|| "fine")
}

dgrnbrg avatar Jun 05 '17 20:06 dgrnbrg

Do you want to try an implementation?

Yamakaky avatar Jun 08 '17 18:06 Yamakaky

See also #69, if you want to do that too.

Yamakaky avatar Jun 08 '17 18:06 Yamakaky