Skip to content

Error type not matching the documentation #101

@tnguyen42

Description

@tnguyen42

I noticed that when printing the error using the onError property on the component, the error was stringified for some reason. Therefore, it was only printing Error.

Furthermore, to access the "actual error", I had to console.log(error.statusCode) or console.log(error.statusMessage), as the documentation indicates that this is the actual shape of the Google Pay error: https://developers.google.com/pay/api/web/reference/error-objects

However, the expected type in this package (@google-pay/button-react/dist/index.d.ts, line 33) expects the standard Error from TypeScript, which doesn't have the statusCode and statusMessage properties, causing the error handling difficult without overwriting the type at least locally.

Unless I mistunderstood something, I think it should instead use a custom error with statusCode and statusMessage properties.

My suggestion would be to define an interface just above the Config interface:

interface GooglePayError extends Error {
    statusCode?: string;
    statusMessage?: string;
}

And using it instead of Error in the Config interface:

interface Config {
    environment: google.payments.api.Environment;
    existingPaymentMethodRequired?: boolean;
    paymentRequest: google.payments.api.PaymentDataRequest;
    onPaymentDataChanged?: google.payments.api.PaymentDataChangedHandler;
    onPaymentAuthorized?: google.payments.api.PaymentAuthorizedHandler;
    onLoadPaymentData?: (paymentData: google.payments.api.PaymentData) => void;
    onCancel?: (reason: google.payments.api.PaymentsError) => void;
-   onError?: (error: Error) => void;
+   onError?: (error: GooglePayError) => void;
    onReadyToPayChange?: (result: ReadyToPayChangeResponse) => void;
    onClick?: (event: Event) => void;
    buttonColor?: google.payments.api.ButtonColor;
    buttonType?: google.payments.api.ButtonType;
    buttonSizeMode?: google.payments.api.ButtonSizeMode;
    buttonLocale?: string;
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions