braintree / braintree-web

A suite of tools for integrating Braintree in the browser
https://developer.paypal.com/braintree/docs/start/hello-client/javascript/v3
MIT License
444 stars 134 forks source link

Unhandled Promise Rejection when creating HostedFields with authorization #669

Closed josemigallas closed 1 year ago

josemigallas commented 1 year ago

General information

Issue description

The following code won't catch the authentication error due to bad client token:

hostedFields.create({ authorization, fields, styles })
  .catch(console.error)

It will instead resolve the promise with the following error in the browser console:

Unhandled Promise Rejection: BraintreeError: Authorization is invalid. Make sure your client token or tokenization key is valid.

Instantiating the client will catch a BraintreeError as expected so I guess this rejection is lost somewhere.

Is this an already known issue? I may be able to send a PR with a fix, provided this is somehow a feature not a bug 😃.

nidhinarendra commented 1 year ago

Hi @josemigallas,

If something causes the client creation to error out (such as a malformed authorization string), Hosted Fields will appear to set up just fine, but you will see an unhandled promise rejection in the console and any method calls that rely on the client will fail with that same error.

If we adjusted the code to bubble up the error from the client creation into the creation of Hosted Fields, it would take away all the speed gains of allowing the Hosted Fields inputs to render and be ready for a customer to enter their card details before the client is actually available.

The unhandled error in the console is a known side effect to pushing the underlying client creation to an asynchronous process. We're aware of it, but the times it actually happens in production are fairly rare. You have to do something like 'intentionally mess up the authorization string' to trigger it on demand.

Let me know if you have more questions on this.

nidhinarendra commented 1 year ago

If there is more that is required on this ticket, then we can either reopen this back or create a new ticket.

Marking this as closed. Thank you.