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

the trait From<anyhow::Error> is not implemented for JsValue #2970

Open
junglie85 opened this issue Jun 29, 2022 · 4 comments
Open

the trait From<anyhow::Error> is not implemented for JsValue #2970

junglie85 opened this issue Jun 29, 2022 · 4 comments
Labels

Comments

@junglie85
Copy link

Summary

This code generates the following error when run with 0.2.81:

#[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
pub fn run() -> anyhow::Result<()> {
error[E0277]: the trait bound `JsValue: From<anyhow::Error>` is not satisfied
  --> src\lib.rs:10:36
   |
10 | #[cfg_attr(target_arch = "wasm32", wasm_bindgen(start))]
   |                                    ^^^^^^^^^^^^^^^^^^^ the trait `From<anyhow::Error>` is not implemented for `JsValue`

From #1742, I was expecting to be able to do this.
How can I use anyhow here?

@ranile
Copy link
Collaborator

ranile commented Jul 1, 2022

I suggest you look at gloo. gloo-utils::error::JsError can be created from JsValue and it implements std::error::Error so it can be used by anyhow

@Liamolucko
Copy link
Collaborator

You can't use anyhow::Result as the return type, but you can use wasm_bindgen::JsError, which should work almost as well. It implements From for anything that implements std::error::Error (including anyhow::Error), which is implicitly used by the ? operator to be able to return any well-behaved error type.

I suggest you look at gloo. gloo-utils::error::JsError can be created from JsValue and it implements std::error::Error so it can be used by anyhow

That's for converting from a JS error to a Rust error, which is the wrong way around here.

@stefnotch
Copy link

@Liamolucko I can't seem to convert an anyhow::Error to a wasm_bindgen::JsError.

e.g. I have a function that currently returns an anyhow error, and so I figured I'd try to manually convert it. That didn't work out.

error[E0277]: the trait bound `anyhow::Error: StdError` is not satisfied
   --> wasm\src\application.rs:182:57
    |
182 |         let mut app = CpuApplication::new().map_err(|e| JsError::from(e))?;
    |                                                         ^^^^^^^ the trait `StdError` is not implemented for `anyhow::Error`, which is required by `JsError: From<anyhow::Error>`
    |
    = help: the trait `From<E>` is implemented for `JsError`
    = note: required for `JsError` to implement `From<anyhow::Error>`

@Liamolucko
Copy link
Collaborator

@Liamolucko I can't seem to convert an anyhow::Error to a wasm_bindgen::JsError.

Oops, I was wrong: anyhow::Error doesn't implement std::error::Error. I'm guessing that's because it there would be a conflict between its From<Self> and From<E> where E: Error impls if it did?

It does implement Deref<Target = dyn Error> though, so you can still convert one to a JsError like so (since &dyn Error implements Error):

let mut app = CpuApplication::new().map_err(|e| JsError::from(&*e))?;

JsError::from just calls e.to_string(), so this doesn't run into any errors about e not living long enough.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants