1

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
  • 2
    dont you think that there should't be any fatal errors? Commented Nov 18, 2011 at 14:20
  • You should make your PHP code more robust so that no untrapped errors occur. Problem solved. Commented Nov 18, 2011 at 14:20
  • Show us the exact code you have tried for set_error_handler(). Commented Nov 18, 2011 at 14:21
  • How do you setup your error handler, please show some code=) Commented Nov 18, 2011 at 14:21
  • @rory: i'm requiring this for local developing :) i'm using some error handling script i found somewhere - please look here: pastebin.com/AKxhC6s9 Commented Nov 18, 2011 at 14:24

6 Answers 6

2

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>
Sign up to request clarification or add additional context in comments.

Comments

2

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

0

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

0

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

0

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).

Comments

0

i found my solution: as i'm expecting a xml to be returned - i only need to check for the content-type of the response, when php raises an error i'm getting text/html instead of text/xml :)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.