Skip to content

[dart2wasm] JS errors can't be caught using an interop type #55481

Open
@srujzs

Description

@srujzs
import 'dart:js_interop';

@JS()
external void eval(String code);

@JS()
external void throwFunction();

void main() {
  eval('''
    self.throwFunction = function() {
      throw new Error('hello world');
    }
  ''');
  try {
    throwFunction();
  } on JSObject catch (e) {
    print(e);
  }
}

DDC and dart2js catch the JS error as it is a subtype of interceptors.JavaScriptObject (and therefore a subtype of JSObject). dart2wasm does not, as its implementation of JS errors is not a subtype of JSObject:

class _JavaScriptError extends Error {
. A proposal is to make that implementation a subtype of JSValue, but it's not clear if a Wasm instruction exists to convert the error to an externref that can then be boxed. Making this a subtype of JSValue also has the other issue that if we replaced JSObject with JSString, dart2wasm would catch the error (because JSString is a JSValue) whereas DDC and dart2js will not.

For now, users should avoid catching JS errors using an interop type for consistency.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-dart2wasmIssues for the dart2wasm compiler.area-web-jsIssues related to JavaScript support for Dart Web, including DDC, dart2js, and JS interop.web-js-interopIssues that impact all js interop

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions