Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[nativescript-stripe] Better error handling #167

Open
sebj54 opened this issue Jan 23, 2023 · 0 comments
Open

[nativescript-stripe] Better error handling #167

sebj54 opened this issue Jan 23, 2023 · 0 comments

Comments

@sebj54
Copy link
Contributor

sebj54 commented Jan 23, 2023

Hi there!

I've been using this plugin for a while, but with a custom patch in order to have a better error handling.

I had an issue in the past on Android where a basic Exception was thrown instead of a StripeException. In this case, calling error.getLocalizedMessage() fails and another exception is thrown.

So I added a _getLocalizedError function, which will test if the error is an instance of StripeException and thus call getLocalizedMessage(). It will also add Stripe's error code and decline code to the error returned.

Here is the function for Android:

_getLocalizedError(error) {
    let localizedError
    if (error instanceof com.stripe.android.exception.StripeException) {
        localizedError = new Error(error.getLocalizedMessage() || error.getMessage());
        if (error.stripeError) {
            localizedError.code = error.stripeError.code;
            localizedError.declineCode = error.stripeError.declineCode;
        }
    } else {
        localizedError = new Error(error.message);
    }
    return localizedError
}

And the one for iOS:

_getLocalizedError(error, intent) {
    const localizedError = new Error(error.localizedDescription);
    if (intent?.lastPaymentError) {
        localizedError.code = intent.lastPaymentError.code;
        localizedError.declineCode = intent.lastPaymentError.declineCode;
    }
    return localizedError
}

Then I replaced all these calls by my function:

- cb(new Error(error.getMessage() || error.getLocalizedMessage()), null);
+ cb(this._getLocalizedError(error), null);

I was wondering if you were interested to add this to the plugin. I would be glad to make a PR in that case :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant