How to use error_chain with generic error types?


#1

I’m trying to use the error_chain library with the std::sync::mpsc queues. I’d like to have the SendError be a foreign_link so that I can use it with .chain_err(), but I’m not sure if there’s any way to specify the foreign link with the generic parameter intact. My code is itself a library that is parameterized by the same type that would parameterize the SendError, which is why I don’t know it in advance.

Thank you!


#2

It should can! But may be crate error-chain has not support this.

Therefor, my solution is ugly:

I think error-chain can support type params by defaulted-type-params.


#3

I haven’t see that custom derive syntax before–is that a part of the error_chain library? Where is it documented?


#4

I found custom in another crate called derive-error-chain,

It said:

extern crate derive_error_chain

#[error_chain(custom)]
#[error_chain(description = r#"|_| "invalid toolchain name""#)]
#[error_chain(display = r#"|t| write!(f, "invalid toolchain name: '{}'", t)"#)]
InvalidToolchainName(String),

equal to

extern crate error_chain

errors {
        InvalidToolchainName(t: String) {
            description("invalid toolchain name")
            display("invalid toolchain name: '{}'", t)
}

#5

Thank you!