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.
Approach | Purpose |
---|---|
Catch exceptions | Ensures that you can provide the proper response when errors occurred |
Get saved information about failures | Check 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.
- Node
- PHP
- Python
- Ruby
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);
});
}
}
$client = new \Payrex\PayrexClient('insert your PayRex Secret API key.');
try {
$paymentIntent = $client->paymentIntents->create([
// incorrect payload
]);
} catch (\Payrex\Exceptions\ResourceNotFoundException $e) {
// handle error if a resource does not exist.
} catch (\Payrex\Exceptions\AuthenticationException $e) {
// handle authentication error
} catch (\Payrex\Exceptions\InvalidRequestException $e) {
// handle error if there's validation error
foreach($e->getError() as $error) {
echo $error->code;
echo $error->detail;
}
}
payrex = PayrexClient('insert your PayRex Secret API key.')
try:
payrex.payment_intents.create(
# incorrect payload
)
except ResourceNotFoundException as e:
# handle error if a resource does not exist.
except AuthenticationInvalidException as e:
# handle authentication error
except RequestInvalidException as e:
# handle error if there's validation error
for error in e.errors:
print(error.code)
print(error.detail)
payrex = Payrex::Client.new("insert your PayRex Secret API key.")
begin
payrex.payment_intents.create(
# incorrect payload
)
rescue Payrex::Errors::ResourceNotFoundError => e
# handle error if a resource does not exist.
rescue Payrex::Errors::AuthenticationInvalidError => e
# handle authentication error
rescue Payrex::Errors::RequestInvalidError => e
# handle error if there's validation error
e.errors.each do |error|
puts error.code
puts error.detail
end
end
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:
- Retrieve a PaymentIntent
- Check if the PaymentIntent encountered a failed payment by checking if
last_payment_error
attribute is not empty. - If the PaymentIntent has the attribute, log the attribute.
- Node
- PHP
- Python
- Ruby
const payrex = require('payrex-node')('insert your PayRex Secret API key.');
const paymentIntent = payrex.paymentIntents.retrieve('insert payment intent id');
// Check paymentIntent
$client = new \Payrex\PayrexClient('insert your PayRex Secret API key.');
$paymentIntent = $client->paymentIntents->retrieve('insert payment intent id');
print "<pre>"
print_r($paymentIntent->last_payment_error)
print "</pre>"
payrex = PayrexClient('insert your PayRex Secret API key.')
payment_intent = payrex.payment_intents.retrieve('insert payment intent id')
# Check payment_intent
payrex = Payrex::Client.new("insert your PayRex Secret API key.")
payment_intent = payrex.payment_intents.retrieve("insert payment intent id")
# Check payment_intent
Types of errors and responses
- Node
- PHP
- Python
- Ruby
Name | Class | Description |
---|---|---|
Resource not found | ResourceNotFoundError | The resource you're retrieving does not exist. |
Authentication error | AuthenticationInvalidError | There is a problem with the authentication credentials passed to PayRex. Check the API key used. |
Invalid request error | RequestInvalidError | There are some potential errors with either the payload or the resource being processed |
Name | Class | Description |
---|---|---|
Resource not found | Payrex\Exceptions\ResourceNotFoundException | The resource you're retrieving does not exist. |
Authentication error | Payrex\Exceptions\AuthenticationException | There is a problem with the authentication credentials passed to PayRex. Check the API key used. |
Invalid request error | Payrex\Exceptions\InvalidRequestException | There are some potential errors with either the payload or the resource being processed |
Name | Class | Description |
---|---|---|
Resource not found | payrex/exceptions/ResourceNotFoundException | The resource you're retrieving does not exist. |
Authentication error | payrex/exceptions/AuthenticationInvalidException | There is a problem with the authentication credentials passed to PayRex. Check the API key used. |
Invalid request error | payrex/exceptions/RequestInvalidException | There are some potential errors with either the payload or the resource being processed |
Name | Class | Description |
---|---|---|
Resource not found | Payrex::Errors::ResourceNotFoundError | The resource you're retrieving does not exist. |
Authentication error | Payrex::Errors::AuthenticationInvalidError | There is a problem with the authentication credentials passed to PayRex. Check the API key used. |
Invalid request error | Payrex::Errors::RequestInvalidError | There are some potential errors with either the payload or the resource being processed |