surf: surf::Exception does not implement std::error::Error

This is an unfriendly choice for errors returned by a library because it makes them not work with ?.

async fn repro() -> Result<(), failure::Error> {
    let _ = surf::get("https://www.rust-lang.org").await?; // doesn't work
    Ok(())
}
async fn repro() -> anyhow::Result<()> {
    let _ = surf::get("https://www.rust-lang.org").await?; // doesn't work
    Ok(())
}

Application-focused error types like failure::Error are built on impl<T: std::error::Error> From<T> which is why it matters that library errors implement std::error::Error.

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 21
  • Comments: 17 (7 by maintainers)

Most upvoted comments

A relatively easy fix for users of eyre (which is a fork of anyhow, so I believe it should also work for the latter) is:

surf::get(my_url).await.map_err(|e| eyre!(e))?
// or
surf::get(my_url).await.map_err(|e| anyhow!(e))?

Alternatively you can also return a eyre::Result<T, surf::Error> (or anyhow::Result<T, surf::Error>) from your functions, although (with eyre at least) you miss out on pretty output that shows where the error occured and possibly more info (depends on the edition of eyre you use).

For what it’s worth, I think http_types::Error existing in the first place was a mistake. It makes sense for user-defined errors in a web server framework, but that’s much more specific than HTTP in general. In my opinion, surf should contain its own error type, probably using something like thiserror. I’d be willing to PR this, and in my experience it is a major usability issue that IMHO would deserve a 3.0.0 on its own.

This seems to still be an issue today. I tried surf 2.0.0-alpha.4, and attempted to use ? in a function using anyhow::Error as its error type. This failed because surf::Error doesn’t implement std::error::Error.

The http_types::Error type in your link does not implement std::error::Error so I’m not sure that would solve it.

That is also my understanding, however the error handling project group is also working on fixing StdError in ways that should help, as far as I’m aware.

We will have a 3.0 at some point and we should think about it for that.

@yoshuawuyts this might want to be marked with 2.0 milestone.