Error Handling
Last updated
fetch('https://api.giftoin.org/api/v1/gcm/reseller/campaign/invalid_id')
.then(response => {
if (!response.ok) {
return response.json().then(errorData => {
throw {
status: response.status,
errorCode: errorData.status.errorCode,
message: errorData.status.errorMessage
};
});
}
return response.json();
})
.then(data => {
// Handle successful response
console.log('Success:', data);
})
.catch(error => {
// Handle error
console.error('Error:', error);
// User-friendly error messages
if (error.errorCode === 5250) {
displayUserMessage("We couldn't find that campaign. Please check the ID and try again.");
} else if (error.errorCode === 5255) {
displayUserMessage("This campaign is out of stock. Please contact support to increase the limit.");
} else if (error.status === 429) {
displayUserMessage("You've made too many requests. Please wait a moment and try again.");
} else {
displayUserMessage("Something went wrong. Please try again later or contact support.");
}
});