Skip to main content

Error Handling

Catch and respond to errors such as invalid data, network problems, etc.

Some of PayRex's approaches to handling errors are in the table below. Regardless of your approach, our responses and recommendations per response can help you handle the errors properly.

ApproachPurpose
Catch exceptionsEnsures that you can provide the proper response when errors occurred
Get saved information about failuresCheck past problems

Catch Exceptions

PayRex SDKs raise an exception whenever an error occurs. Suppose an immediate problem prevents an API call from continuing. In that case, it's best practice to catch and handle exceptions.

To catch an exception, use the try/catch syntax. PayRex provides exception classes you can use to catch exceptions. Each class represents a different kind of error.

const payrex = require('payrex-node')('insert your PayRex Secret API key.');

try {
const event = await payrex.paymentIntents.create({
// incorrect payload
});
} catch (error) {
if (error.name === 'ResourceNotFoundError') {
// handle error if a resource does not exist.
} else if (error.name === 'AuthenticationInvalidError') {
// handle authentication error
} else if (error.name === 'RequestInvalidError') {
// handle error if there's validation error
error.errors.forEach((e) => {
console.log(e.code);
console.log(e.detail);
});
}
}

Get saved information about failures

Some PayRex resources store information about failures. If something went wrong, you can access and learn more from this information.

Example:

  1. Retrieve a PaymentIntent
  2. Check if the PaymentIntent encountered a failed payment by checking if last_payment_error attribute is not empty.
  3. If the PaymentIntent has the attribute, log the attribute.
const payrex = require('payrex-node')('insert your PayRex Secret API key.');

const paymentIntent = payrex.paymentIntents.retrieve('insert payment intent id');

// Check paymentIntent

Types of errors and responses

NameClassDescription
Resource not foundResourceNotFoundErrorThe resource you're retrieving does not exist.
Authentication errorAuthenticationInvalidErrorThere is a problem with the authentication credentials passed to PayRex. Check the API key used.
Invalid request errorRequestInvalidErrorThere are some potential errors with either the payload or the resource being processed