Catching requestCredentials exception prevents Forge consent flow

Hello, I have a Forge UI Button component which calls a function when clicked. Inside this function is the typical oauth client credential call like so:

  if(!await provider.hasCredentials()){
    await provider.requestCredentials();
  };

The problem is the requestCredentials call relies on throwing an exception in order to initiate the oauth code flow - and thus I cannot catch any exceptions that my function may raise. How can I catch exceptions from my function without preventing the oauth flow by Forge?

Thanks,
Scott

This is an unfortunate side effect of requiring the invocation to stop and ask for the user’s consent.

You can catch everything but the authentication exceptions by checking the exception properties like this:

try {
  await actionThatMightRequireCredentials();
} catch (e) {
  if (e?.name === 'NEEDS_AUTHENTICATION_ERR') {
    throw e;
  }
  // Your regular error handling
}

Thanks for the report, we will investigate how to make the check more intuitive and document this better.

Thank you, Alexey. I was re-throwing the exception but wasn’t sure if I was missing something!

Scott