Code Monkey home page Code Monkey logo

Comments (3)

RReverser avatar RReverser commented on June 29, 2024 1

That looks reasonable, although note that Box<dyn std::error::Error> will be still prone to the same problem with erased backtrace (since it's an erased generic Rust error type).

Maybe store actual serde_wasm_bindgen::Error there instead?

Also note that you can use cfg on enum branches so the whole thing might be simplified to:

#[derive(Debug, Error)]
pub enum MyError {
	#[cfg(target_arch = "wasm32")]
    SerdeWasmBindgen(serde_wasm_bindgen::Error),
    // ...regular errors
}

This should also let Rust automatically implement Send on your error enum when possible (non-wasm32 targets), and !Send when not (wasm32) instead of you overriding it via manual unsafe impl.

from serde-wasm-bindgen.

RReverser avatar RReverser commented on June 29, 2024

In general, JsError / JsValue-based error is more useful in Wasm-related code because it creates a JS error with .stack correctly tracked by the JS engine and associated with the original thrown location inside Wasm. Rust backtraces can't provide that level of detail in Wasm, so creating errors from string at the last stage makes them much less useful for actual debugging.

That's why I prefer to do the opposite and convert JsError to string only when I really want to go from JS error to a Rust error and I'm okay with stripping the backtrace.

Most of the time, you also use serde-wasm-bindgen at the top level of the function exposed via #[wasm_bindgen] anyway, so as long as you're propagating it immediately to Result<..., JsValue>, the fact that JS values are not thread-safe shouldn't be a problem.

I wonder what your usecase is where this doesn't work.

from serde-wasm-bindgen.

regexident avatar regexident commented on June 29, 2024

In general, JsError / JsValue-based error is more useful in Wasm-related code because it creates a JS error with .stack correctly tracked by the JS engine and associated with the original thrown location inside Wasm. Rust backtraces can't provide that level of detail in Wasm, so creating errors from string at the last stage makes them much less useful for actual debugging.

That does indeed make a lot of sense!

I'm working on a cross-platform project that among several native targets also supports the web via wasm.

We're basically wrapping individual errors into a wrapper-enum error type in order to provide more fine-grained internal error-handling via thiserror.

It looks like I managed to make things work with this change:

Before:

#[derive(Debug, Error)]
pub enum MyError {
    SerdeWasmBindgen(Box<dyn std::error::Error + Send>),
    // ...
}

#[cfg(not(target_arch = "wasm32"))]
unsafe impl Send for MyError {}

After:

#[cfg(target_arch = "wasm32")]
type BoxedStdError = Box<dyn std::error::Error>;

#[cfg(not(target_arch = "wasm32"))]
type BoxedStdError = Box<dyn std::error::Error + Send>;

#[derive(Debug, Error)]
pub enum MyError {
    SerdeWasmBindgen(BoxedStdError),
    // ...
}

#[cfg(not(target_arch = "wasm32"))]
unsafe impl Send for MyError {}

from serde-wasm-bindgen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.