During my testing of STRIPE in a website, I built the code like this:
try {
$charge = Stripe_Charge::create(array(
\"amount\" => $cli
This is how Stripe catches errors: Documentation.
try {
// make Stripe API calls
} catch(\Stripe\Exception\ApiErrorException $e) {
$return_array = [
"status" => $e->getHttpStatus(),
"type" => $e->getError()->type,
"code" => $e->getError()->code,
"param" => $e->getError()->param,
"message" => $e->getError()->message,
];
$return_str = json_encode($return_array);
http_response_code($e->getHttpStatus());
echo $return_str;
}
You can then catch the error in ajax with the following code:
$(document).ajaxError(function ajaxError(event, jqXHR, ajaxSettings, thrownError) {
try {
var url = ajaxSettings.url;
var http_status_code = jqXHR.status;
var response = jqXHR.responseText;
var message = "";
if (isJson(response)) { // see here for function: https://stackoverflow.com/a/32278428/4056146
message = " " + (JSON.parse(response)).message;
}
var error_str = "";
// 1. handle HTTP status code
switch (http_status_code) {
case 0: {
error_str = "No Connection. Cannot connect to " + new URL(url).hostname + ".";
break;
} // No Connection
case 400: {
error_str = "Bad Request." + message + " Please see help.";
break;
} // Bad Request
case 401: {
error_str = "Unauthorized." + message + " Please see help.";
break;
} // Unauthorized
case 402: {
error_str = "Request Failed." + message;
break;
} // Request Failed
case 404: {
error_str = "Not Found." + message + " Please see help.";
break;
} // Not Found
case 405: {
error_str = "Method Not Allowed." + message + " Please see help.";
break;
} // Method Not Allowed
case 409: {
error_str = "Conflict." + message + " Please see help.";
break;
} // Conflict
case 429: {
error_str = "Too Many Requests." + message + " Please try again later.";
break;
} // Too Many Requests
case 500: {
error_str = "Internal Server Error." + message + " Please see help.";
break;
} // Internal Server Error
case 502: {
error_str = "Bad Gateway." + message + " Please see help.";
break;
} // Bad Gateway
case 503: {
error_str = "Service Unavailable." + message + " Please see help.";
break;
} // Service Unavailable
case 504: {
error_str = "Gateway Timeout." + message + " Please see help.";
break;
} // Gateway Timeout
default: {
console.error(loc + "http_status_code unhandled >> http_status_code = " + http_status_code);
error_str = "Unknown Error." + message + " Please see help.";
break;
}
}
// 2. show popup
alert(error_str);
console.error(arguments.callee.name + " >> http_status_code = " + http_status_code.toString() + "; thrownError = " + thrownError + "; URL = " + url + "; Response = " + response);
}
catch (e) {
console.error(arguments.callee.name + " >> ERROR >> " + e.toString());
alert("Internal Error. Please see help.");
}
});