http-rs / http-types

Common types for HTTP operations
https://docs.rs/http-types
Apache License 2.0
200 stars 83 forks source link

Allow adding context to Result similarly to other Result types #367

Open maxbrunsfeld opened 3 years ago

maxbrunsfeld commented 3 years ago

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?

Fishrock123 commented 3 years ago

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