Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

? on surf::Exception #35

Closed
abreis opened this issue Oct 30, 2019 · 3 comments
Closed

? on surf::Exception #35

abreis opened this issue Oct 30, 2019 · 3 comments

Comments

@abreis
Copy link

abreis commented Oct 30, 2019

I'm working on a simple app using async/await and Surf, and I can't seem to use it with anyhow.

Here's a minimal example:

async fn some_request() -> anyhow::Result<()> {
    let _response = surf::get("https://www.rust-lang.org").await?;
    Ok(())
}

cargo +beta check complains with:

error[E0277]: the size for values of type `dyn std::error::Error + std::marker::Send + std::marker::Sync` cannot be known at compilation time
 --> src/main.rs:8:64
  |
8 |     let response = surf::get("https://www.rust-lang.org").await?;
  |                                                                ^ doesn't have a size known at compile-time
  |
  = help: the trait `std::marker::Sized` is not implemented for `dyn std::error::Error + std::marker::Send + std::marker::Sync`
  = note: to learn more, visit <https://doc.rust-lang.org/book/ch19-04-advanced-types.html#dynamically-sized-types-and-the-sized-trait>
  = note: required because of the requirements on the impl of `std::error::Error` for `std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>`
  = note: required because of the requirements on the impl of `std::convert::From<std::boxed::Box<dyn std::error::Error + std::marker::Send + std::marker::Sync>>` for `anyhow::Error`
  = note: required by `std::convert::From::from`

I can't tell if this is an issue with anyhow, with surf, with the .await, or if I'm doing something wrong.

@dtolnay
Copy link
Owner

dtolnay commented Oct 30, 2019

This is because Surf is using a silly choice of error type. :( http-rs/surf#86
Libraries should return errors that implement std::error::Error in order for ? to work, and Surf's does not.

For now you can work around this by avoiding ?:

use anyhow::{bail, Result};

async fn some_request() -> Result<()> {
    let _response = match surf::get("https://www.rust-lang.org").await {
        Ok(response) => response,
        Err(err) => bail!(err),
    };

    ...
}

@dtolnay dtolnay changed the title anyhow and async fn ? on surf::Exception Oct 30, 2019
@abreis
Copy link
Author

abreis commented Oct 30, 2019

Thanks!

@mchlrhw
Copy link

mchlrhw commented Jun 16, 2020

I know this is closed, but I found myself here recently so I thought I'd share another approach.
If you want to add a context to surf errors, or want to make use of ?, you can do:

use anyhow::{anyhow, Context, Result};

async fn some_request() -> Result<()> {
    let _response = surf::get("https://www.rust-lang.org")
        .await
        .map_err(|err| anyhow!(err)) // TODO: Remove me when surf 2.0 is released
        .context("Failed to fetch from rust-lang.org")?;

    ...
}

benmkw added a commit to benmkw/signify-rs that referenced this issue Sep 1, 2020
Repository owner locked and limited conversation to collaborators Jan 24, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants