4

I'm implementing a client based on subxt, and have difficulties with subxt errors:

the trait `From<Result<std::option::Option<u32>, subxt::Error>>` is not implemented for `Result<std::option::Option<u32>, anyhow::Error>`

I see that subxt's error type derives from thiserror which should implement std::error::Error trait.

#[derive(Debug, thiserror::Error)]
pub enum Error {
    // [...]

From anyhow side, it should accept any type implementing the std::error::Error trait.

Is there some particular configuration of #[subxt::subxt(_____)] for error type to derive error trait?

1 Answer 1

3

I've repro'd the error with a minimal example, and although the conversion subxt::Error -> anyhow::Error is implemented, it is not implemented when wrapped in a result. Thus, you can either manually use map_err or the ? operator.

use anyhow;
use subxt;

fn failable_func() -> Result<Option<u32>, anyhow::Error> {
    let err = subxt::Error::Other("Here's an error".to_string());
    let res: Result<Option<u32>, subxt::Error> = Err(err);
    // The line below would fail to compile, because result -> result
    // let err2 = res.into()
    // But we can manually map the error without issues
    let err2: Result<Option<u32>, anyhow::Error> = res.map_err(|e| e.into());
    // This would also work
    let inner = res?;
    Ok(inner)
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.