Code Monkey home page Code Monkey logo

Comments (7)

PaulKinlan avatar PaulKinlan commented on September 26, 2024 1

from comlink.

michael42 avatar michael42 commented on September 26, 2024

I'd say it mostly works, but doesn't keep all information of the thrown object.

For example, I'm using PouchDB, which throws {status: 404}. I'm running a slightly patched comlink for a few months now that retains all enumerable properties and the error message and stack (if there is one):

--- a/node_modules/comlinkjs/comlink.js
+++ b/node_modules/comlinkjs/comlink.js
@@ -27,9 +27,9 @@ const proxyTransferHandler = {
 };
 const throwTransferHandler = {
     canHandle: (obj) => obj && obj[throwSymbol],
-    serialize: (obj) => obj.toString() + "\n" + obj.stack,
+    serialize: (obj) => Object.assign({}, obj, {message: obj.message, stack: obj.stack}),
     deserialize: (obj) => {
-        throw Error(obj);
+        throw Object.assign(new Error(), obj);
     }
 };
 export const transferHandlers = new Map([

from comlink.

surma avatar surma commented on September 26, 2024

@michael42 Good catch. You mind opening a PR? I just wanna be on the safe side legally β€” I don’t think I can just rip your code from your comment ;)

from comlink.

surma avatar surma commented on September 26, 2024

@PaulKinlan What kind of best practices are you looking for? The goal was for Comlink to behave as transparently as possible.

from comlink.

PaulKinlan avatar PaulKinlan commented on September 26, 2024

I know what the issue was now. You can't create a Symbol on a string, so when a library does throw 'sumfins wrong'; Comlink will throw an error like so Cannot create property 'Symbol(throw)' on string

from comlink.

felixfbecker avatar felixfbecker commented on September 26, 2024

Note that this won't work:

throw Object.assign(new Error(), obj);

if you have code like this:

} catch (error) {
  throw Object.assign(new Error('Something went wrong:' + err.message), { error })
}

because error will not be transferrable and throw when trying to post the message

from comlink.

surma avatar surma commented on September 26, 2024

This is now documented in the README.

from comlink.

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.