在jQuery AJAX中捕获PHP错误体

I have an AJAX build that functions like a CMD line box. It allows me to breakup and scrub 100,000+ line CSV files on servers where MySQL 'IMPORT from FILE' is disabled. That "scrub" process is different for every client. Therefore I have built this tool to allow me to include various PHP scripts

It works great except for error handling in 1 area: the PHP error level.

I want to log the error using JS, specifically console.log()

consider then the following JS

$.ajax({
        type: 'POST', 
        dataType: 'text', //also tried json
        url: PHP_SCRIPT, //for sake of referance
        data: $.param(data), // param object values as POST values
        cache: false, 
        error: function(error) { 
            console.log("fubar:" +  JSON.stringify(error));

If I cause an error in PHP_SCRIPT (that is not handled using try/catch and output as JSON) then I get the following "ambiguous" reply

stringify:{"readyState":4,"responseText":"","status":500,"statusText":"error"}

Here is the problem: responseText is empty.

What is really happening in PHP_SCRIPT is this error:

Fatal error: Uncaught UnexpectedValueException: RecursiveDirectoryIterator::

Which I can of course see if I run the PHP script (and I know why its happening, my question is not about the RDI error). Consider it could be other errors as well: a failed include, a mistake in code, ect. But JS and jQuery AJAX do not seem to "capture" the body of the failed PHP script.

GOAL: I want to "capture" PHP errors and show them using console.log() (or even my makeshift CMD line box) so I do not have to cut up the PHP_SCRIPT's and debug each line separately. I do not understand why error.responseText does not capture the output.

Ideally - the PHP "Fatal error" above should have been captured as TEXT and output in the log.

Note: I have PDO try/catch handling for the DB queries where I can output a success.error object and handle it appropriately, catching the PDO exception and log it to the console. Alas, I see no useful way to handle other PHP errors (such as a failed include or other common PHP mistakes). If it matters- I am using WordPress Admin AJAX with nonce and die() and my scripts work great, but during dev of new scripts catching errors is annoying.

Question Summary:

  1. Is there a way to catch all/any PHP errors that are not output as JSON and console.log them when $.ajax - error happens?
  2. Is there some way to capture the 'body' of the PHP error and console.log it?

Thank you for your consideration in this matter

UPDATE--- Added video to clarify: http://www.screencast.com/t/ZyCeaMyAxBO

Something like this will capture all uncaught exceptions and output a message in JSON format.

set_exception_handler(function($e) {
    $msg = "Error: ";
    // maybe you want to treat some differently?
    if ($e instanceof \PDOException) {
        $msg = "Database error: ";
    }
    // you can access all properties of the exception to build a reply
    $msg .= $e->getMessage();
    header("Content-Type: text/json");
    echo json_encode(["message" => $msg]);
});

You can't catch ALL error situations, basically because you'll have to write the error handler in PHP and some errors (like parse errors) cause that the script doesn't even compile and therefor cannot be executed at all.

But with set_error_handler() / set_exeception_handler() you can probably cover a good portion...