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

Error objects lost during serialization #234

Closed
emileber opened this issue Jun 22, 2023 · 5 comments
Closed

Error objects lost during serialization #234

emileber opened this issue Jun 22, 2023 · 5 comments
Assignees

Comments

@emileber
Copy link

Context

When using @remote-ui/rpc or any package using it, built-in error objects are serialized as {}, which is unhelpful as any relevant information about errors happening in any function on the other side (iframe, web worker, etc) is lost when it reaches the host.

Hypothesis

Since built-in error object keys aren't enumerable, when an error instance gets serialized, it detects no keys and gets serialized as {}.

When looking at the @remote-ui/rpc package's code, it seems to confirm my assumption:

      if (isBasicObject(value)) {
        seen.set(value, [undefined]);

        const transferables: Transferable[] = [];
        const result = Object.keys(value).reduce((object, key) => {

Possible solutions

According to MDN's page on Error:

Error is a serializable object, so it can be cloned with structuredClone() or copied between Workers using postMessage().

What if we included a new case where we could treat instanceof Error as if it was a primitive to be transferred as-is?

Or would it make sense to switch to Object.getOwnPropertyNames() instead of Object.keys to get non-enumerable string properties as well?

Object.keys(error)
// => []
Object.getOwnPropertyNames(error)
// => ['stack', 'message']
@emileber
Copy link
Author

For visibility and feedback: @lemonmade @robin-drexler @kencwng

@lemonmade
Copy link
Member

I'd be in favor of special-casing Error instances, although it does seem odd to return an error instead of throwing it (which is already handled, for functions proxied via @remote-ui/rpc).

@emileber
Copy link
Author

Good point, I didn't notice that errors were properly handled when thrown and just not serialized when passed around. In my specific case, I could probably get around this by changing our implementation to throw instead.

Though serializing error objects shouldn't hurt, right?

@emileber emileber self-assigned this Jun 29, 2023
@emileber
Copy link
Author

@emileber
Copy link
Author

PR #255 might have helped or even fix this, I need to test again to see.

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

No branches or pull requests

2 participants