Handling a fatal error in PHP from an AJAX call

Quite simply, I want my javascript to be able to react to a fatal 500 error caused by PHP, appropriately to the error.My goal is simply to collect the fatal error message that the script produced, so I can show it on the client side.Is this in any way possible?EDIT: cleaned up the question, in case it gets, …

via PHP Website Development » Search Results » ajax:

Handling a fatal error in PHP from an AJAX call

Quite simply, I want my javascript to be able to react to a fatal 500 error caused by PHP, appropriately to the error.
My goal is simply to collect the fatal error message that the script produced, so I can show it on the client side.
Is this in any way possible?
EDIT: cleaned up the question, in case it gets, by chance, it gets searched.
………………………….

Processing Fatal Errors in PHP:
register_shutdown_function(“foo”);
function foo()
$e = error_get_last();
if($e & (E_ERROR
By checking for all fatal errors, you won’t trigger the handle for notices or warnings that happened earlier, but didn’t stop execution of the script. Hopefully you already handle those.
Side note:
Handling fatal errors using jQuery:
$(document).ajaxError( function(e, xhr, settings, exception)
[handle 500 error. xhr.responseText will contain the printed text.]
);
$.post( […] );
See http://api.jquery.com/ajaxError/ for more details.
Final Note: display errors should be off if you wish to parse JSON, or text will appear before the JSON encoded object.
………………………….

Check out the .status for an AJAX request.
ajaxRequest = new XMLHttpRequest();

ajaxRequest.onreadystatechange = function()

if(ajaxRequest.readyState == 4 && ajaxRequest.status == 500)

//do something

}

ajaxRequest.open(“GET”,”path/file.php?var=variable”);
ajaxRequest.send();
^in a nutshell
………………………….

I provided this code to another user, that would be your solution here. Just use the Jquery 1.5 XHR object to receive error states and if it is an error state, to something with it from your javascript code:

………………………….

You should never show any sort of server error to the client! This is a huge security problem. Perhaps, if it is a protected page and only developers would get the information it wouldn’t be a problem, but for normal users you should not divulge any info. See OWASP: Error Handling for more details.
Also, Fatal Error’s are something that you, as a developer, should be able to prevent; they represent something you’re doing in code that is very wrong. If your production code regularly fails with fatal errors, then you have much larger problems on your hands than the actual errors themselves.
And I could be wrong, but I think that during the shutdown process (such as for register_shutdown_function()), I think it is too late to send output to the browser.
EDIT:
From the OP:
My goal is simply to collect the fatal error message that the script produced, so I can show it on the client side.
My first sentence:
You should never show any sort of server error to the client!
I don’t know how to make that any more clear.
Let me also reiterate a different point with new wording:
You should not be getting fatals in live code, and definitely not to the point of needing a GUI for it. Log your errors, and for development, follow them with tail -f (I haven’t seen one of my own PHP error messages in a browser in years). SSH for remote log viewing. Otherwise, turn error display off.

For more info: Handling a fatal error in PHP from an AJAX call

PHP Website Development » Search Results » ajax

Handling a fatal error in PHP from an AJAX call

Share this post:

Related Posts

Leave a Comment