dart-lang / sdk

The Dart SDK, including the VM, JS and Wasm compilers, analysis, core libraries, and more.
https://dart.dev
BSD 3-Clause "New" or "Revised" License
10.21k stars 1.57k forks source link

Add documentation on how to catch JS interop errors #55496

Open srujzs opened 6 months ago

srujzs commented 6 months ago

Related bug: https://github.com/dart-lang/sdk/issues/55481

Currently, dart2wasm catches all JS exceptions as a singular error type:

https://github.com/dart-lang/sdk/blob/ad95bb6f89eb3aa36b129320fba47c29574b7084/sdk/lib/_internal/wasm/lib/errors_patch.dart#L40

This is a private type, so we don't have a good way to determine if this was a JS error (besides toString).

DDC and dart2js use interceptors to differentiate between JS errors and to allow users to use a JS interop interface to interact with the error.

Due to these inconsistencies, some ways of catching JS errors will work in ddc and dart2js, but fail in dart2wasm. We should add documentation to dart.dev on how we expect users to catch JS errors and inspect the error (if even possible).

It seems like the only consistent way to do this today is to just catch it as an arbitrary error:

try {
  window.throwAnError(...);
} catch (e) {
  ...
}

cc @MaryaBelanger

limcheekin commented 4 months ago

Please include code snippets on how to test it, for example:

    testWidgets('JavaScript function will throws error',
        (WidgetTester tester) async {
      expect(
        () async {
          await javaScriptFunction();
        },
        throwsA(...),
      );
    });
srujzs commented 4 months ago

Ack. I suspect the best we can do today is either expect an Error or a JSObject (which I think is the case when it's just a JS Error in DDC).