Error Handling
The WapiPay API uses standard HTTP status codes to indicate the success or failure of an API request.
Understanding these codes and implementing appropriate error-handling strategies will help ensure that your application can gracefully handle issues and provide meaningful feedback to users.
Implementing Error Handling
To handle errors effectively, you should check the HTTP status code returned by the API and take appropriate action based on the type of error.
Example: Making a GET Request with Error Handling
import requests
url = 'https://api-sandbox.aiprise.com/api/v1/your-endpoint'
headers = {
'Authorization': 'Bearer YOUR_API_KEY'
}
response = requests.get(url, headers=headers)
if response.status_code == 200:
print('Success:', response.json())
elif response.status_code == 400:
print('Bad Request:', response.json())
elif response.status_code == 401:
print('Unauthorized:', response.json())
elif response.status_code == 403:
print('Forbidden:', response.json())
elif response.status_code == 404:
print('Not Found:', response.json())
else:
print('Error:', response.status_code, response.json())
const axios = require('axios');
const url = 'https://api-sandbox.aiprise.com/api/v1/your-endpoint';
const headers = {
'Authorization': 'Bearer YOUR_API_KEY'
};
axios.get(url, { headers })
.then(response => {
console.log('Success:', response.data);
})
.catch(error => {
if (error.response) {
// The request was made and the server responded with a status code
switch (error.response.status) {
case 400:
console.log('Bad Request:', error.response.data);
break;
case 401:
console.log('Unauthorized:', error.response.data);
break;
case 403:
console.log('Forbidden:', error.response.data);
break;
case 404:
console.log('Not Found:', error.response.data);
break;
default:
console.log('Error:', error.response.status, error.response.data);
}
} else {
// The request was made but no response was received
console.log('Error:', error.message);
}
});
Updated 7 months ago