Error Handling in WebUPI

As with any application, error handling is an essential part of using WebUPI. Errors can occur at various stages of the payment process, and it's important to handle these errors gracefully to provide a good user experience.

Handling Errors with webUPI.enable()

The webUPI.enable() method is used to prompt the user for permission to use their UPI capabilities. If the user denies permission or cancels the prompt, an error will be thrown. Here's an example of how to handle this error:


try {
    await webUPI.enable();
} catch (error) {
    // User denied permission or cancelled
    console.log(error);
}

In this example, if an error is thrown, the error message is logged to the console. In a real-world application, you would likely want to display a user-friendly error message to the user.

Handling Errors with webUPI.sendPayment()

The webUPI.sendPayment() method is used to request that the user sends a payment for a UPI URI. If an error occurs during this process, an error will be thrown. Here's an example of how to handle this error:


try {
    const upiURI = "upi://pay?pa=example@upi&pn=Example&mc=0000&tid=000000000000&tr=TestTransaction&tn=TestTransaction&am=10.01&cu=INR&url=http://example.com";
    await webUPI.sendPayment(upiURI);
} catch (error) {
    // Payment failed
    console.log(error);
}

In this example, if an error is thrown, the error message is logged to the console. As with the webUPI.enable() method, you would likely want to display a user-friendly error message to the user in a real-world application.

Error Messages

When an error occurs, WebUPI will throw an error with a message that provides some information about what went wrong. Here are some of the error messages that you might encounter:

  • "User denied permission": The user denied permission to use their UPI capabilities.

  • "Payment failed": The payment process failed.

  • "Invalid UPI URI": The provided UPI URI was invalid.

These are just a few examples. The actual error messages you encounter may vary based on the specific circumstances of the error.

Remember, error handling is a critical part of developing a robust application. By handling errors gracefully, you can ensure that your users have a smooth and enjoyable experience when using your website.

Last updated