duanlan3598 2018-09-26 19:59
浏览 70
已采纳

在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

  • 写回答

2条回答 默认 最新

  • doucai6663 2018-09-26 20:38
    关注

    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]);
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?