i am using jquery's $.post and the server script sometimes will raise a php error.
any ideas how i could catch that error and report it back to the client script?
i have tried set_error_handler() but unfortunately for some reason it doesn't work on fatal php errors, so when there's an error simply nothing will happen. thanks
6 Answers
The best way to do this is to have the script always return json and have a success:true/false in it, then you will never need the error handler.
I know you can't always do that so according to the docs you can do this found here:
var jqxhr = $.post("example.php", function() {
alert("success");
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });
// perform other work here ...
// Set another completion function for the request above
jqxhr.complete(function(){ alert("second complete"); });
Update
With xml:
<root>
<success>true/false</success>
<response>
<response_node_1>Foo</response_node_1>
<response_node_2>Bar</response_node_2>
</response>
</root>
Comments
You can't track fatal errors from within the same PHP script. As you write set_error_handler is not working on those. There is not much you can do about it but look for the HTTP response code (500 - Internal Server Error).
What you can do is to make your code more failsafe/devensive/robust so fatal errors don't appear. Do this by analysing your php error log which fatal errors appear and look inside the related code why they appear and how you can prevent these with correct error checking.
Comments
You can set up global error handling by using something like
$.ajaxSetup({
error:function(x,e){
if(x.status==0){
alert('You are offline!!\n Please Check Your Network.');
}else if(x.status==404){
alert('Requested URL not found.');
}else if(x.status==500){
alert('Internel Server Error.');
}else if(e=='parsererror'){
alert('Error.\nParsing JSON Request failed.');
}else if(e=='timeout'){
alert('Request Time out.');
}else {
alert('Unknow Error.\n'+x.responseText);
}
}
});
Or in the json response look at the error handler and decide what to do when it is passed with certain variables.
Comments
You don't want scripts to output error information in production environments, so you should disable error reporting there.
What kind of errors are you talking about? In your development environment, enable all errors and track them down, and program defensively to prevent errors from happening. There are many ways of doing this, depending on where the error comes from.
Finally, if you can trap errors, you could set some kind of error flag ($error = true), and on the final output, perform something like this:
echo '{"success": $error}';
if (!$error)
{
echo $the_requested_data;
}
You can then let jQuery read the success variable.
Comments
Depending on php configuration fatal errors will sometime (thanks PHP) result in an HTTP 200 response that will not be catched by jQuery as error but will be rather seen as a good response. So, as someone was already pointing, you should avoid having fatal errors (double checking error-prone parts of your code, catching exceptions, checking array indexes, declaring variables before using and so on).
set_error_handler().