WebAssembly / WebAssembly/component-model

Top-level JS error mappings

Open
#121 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
WebAssembly
Stars
1.4k
Forks
130
Avg merge
2d 1h
Merged PRs (30d)
15

Description

For functions that directly return a result, is the expectation that these map to the host-level error handling? This is mentioned briefly as a possibility for JS results in the JS API embedding explainer section.

The possibilities seem to be:

  1. Do nothing - the function called in JS just returns the tagged object - { tag: 'err', value: err_value }
  2. Throw top-level errors directly - the function called in JS throws the top-level result object error when there is an error and returns the value directly. This seems like it is similar to the Promise situation as well? - throw err_value or return success_value.
  3. Wrap top-level errors with a JS Error - the function first wraps the error in a JS error class before throwing the error - throw new Error(err_value).

It seems at first glance like a first-class error wrapping would be a nice design to handle the host integration, but (2) and (3) are both quite limited.

throw value per (2) does work pretty consistently for any value, and retains the one to one properties of the component model, but then there is no way to throw a native JS error type, which may be seen as weird from a user point of view (despite the fact that throw { some: 'data' } gives the most structured type of error logs!).

(3) would result in all top-level error values running through JS toString semantics and is then lossy from a component model value perspective, so then perhaps leads to thinking more carefully about the exact toString semantics of all data types to possibly be less lossy and avoid the infamous [object Object] kind of outputs. Note that records and tuples is currently specified to give [object Record] so doesn't help there much either!

From a high-level perspective, if building code for JS you'd expect to be able to throw nice errors to JS, for some definition of nice.

On the lifting side, when a component calls an imported function that throws, should we try and implement symmetric behaviours?

Another option might be to do something like throw Object.assign(new Error(String(err_value)), { value: err_value }) where the value is attached as a property of the error so that it can always be retrieved manually or otherwise, then to possibly even maintain this check so that arbitrary errors can be constructed for a symmetrical lifting.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the JS API embedding explainer section referenced in the issue, then compare its treatment of directly returned results with the lifting side where imported functions throw. Resolve whether errors are returned, thrown directly, wrapped, or preserved on an Error, including symmetric behavior; done means the host-integration behavior is specified clearly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, wasm
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.