Skip to content

JS exceptions are hidden from developer when using Promises (async functions) #4045

@mkonicek

Description

@mkonicek

Take the following example, which (correctly) produces a redbox:

IntentAndroid.canOpenURL(url, (canOpen) => {
  // Some code that can throw ...
  throw new Error('This should redbox');
});

The Java signature is:

@ReactMethod
public void canOpenURL(String url, Callback callback)

Now change this to an async function:

@ReactMethod
public void canOpenURL(String url, Promise promise)

IntentAndroid.canOpenURL(url).then((canOpen) => {
  // Some code that can throw ...
  throw new Error('This should redbox');
});

No redbox is shown and nothing is logged which is in my opinion very poor developer experience. We've all been there - you're trying to debug a program which doesn't work but since there are no error messages and you have no idea what's wrong.

The way to "fix" this is to call done():

IntentAndroid.canOpenURL(url).then((canOpen) => {
  // Some code that can throw ...
  throw new Error('This should redbox');
}).done();  // Now throws and produces a redbox

But this is super easy to forget :(

For now, I'll go with a callback in the IntentAndroid. Feel like we need to find a good solution to this problem first before we migrate all APIs to async functions.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions