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 ROS Turtlebot3 多机协同自主探索环境时遇到的多机任务分配问题,explore节点
  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题