http-types icon indicating copy to clipboard operation
http-types copied to clipboard

Allow adding context to Result similarly to other Result types

Open maxbrunsfeld opened this issue 4 years ago • 1 comments

When using anyhow directly, you can add context to any fallible operation using the methods from the anyhow::Context trait, which is implemented for all result types whose error implements std::error::Error. This is very useful feature of anyhow.

my_fallible_operation()
    .await
    .context("my fallible operation failed")?;

But when using http_types::Result, you can't use anyhow::Context, because http_types::Error does not implement std::error::Error.

I can see why it would be problematic to implement std::error::Error for http_types::Error - it looks like it would conflict with some existing trait implementations. And unfortunately, anyhow::Context uses the sealed-trait pattern, so http_types::Error can't implement that trait either.

What do you think about providing identical methods as anyhow::Context directly for http_types::Result, maybe via a trait like http_types::Context, which http_types::Result would implemented?

maxbrunsfeld avatar Jun 04 '21 20:06 maxbrunsfeld

eyre also has something like this, we should probably add something similar.

Fishrock123 avatar Jun 07 '21 23:06 Fishrock123