surf icon indicating copy to clipboard operation
surf copied to clipboard

The error type does not always work with ?

Open ghost opened this issue 5 years ago • 1 comments

Example:

async fn fetch(url: String, sender: Sender<String>) -> anyhow::Result<()> {
    let body = surf::get(&url).recv_string().await?;
    sender.send(body).await;
    Ok(())
}

Error:

  |
9 |     let body = surf::get(&url).recv_string().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`

Workaround:

async fn fetch(url: String, sender: Sender<String>) -> anyhow::Result<()> {
    let body = surf::get(&url)
        .recv_string()
        .await
        .map_err(|e| anyhow!(e))?;
    sender.send(body).await;
    Ok(())
}

I think the proper solution is for the error type (Exception) to be a dedicated type that implements std::error::Error.

ghost avatar Apr 01 '20 18:04 ghost

See #86 for some previous discussion on this issue.

abreis avatar Apr 02 '20 10:04 abreis