dongzhuo1930 2014-01-12 12:07
浏览 26

PHP错误/异常处理 - 隐藏错误但处理它?

I am working on an API server and I'd like to handle all PHP errors and exceptions. So, when ever I make a request to my API Server, the response will always be consistent - even during a fatal php error or some internal exception from my other class.

I came up with the following:

// Register Custom Error Handler
error_reporting(E_ALL);
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', 'logs/php-errors.log');
function ErrorHandler($errno, $errstr, $errfile, $errline) {
    HandleError($errstr, $errfile, $errline);
}
function ExceptionErrorHandler(Exception $e) {
    HandleError($e->getMessage(), $e->getFile(), $e->getLine());
}
function ShutdownFunction() {
    $error = error_get_last();
    if (!is_null($error) && sizeof($error)) {
        HandleError($error['message'], $error['file'], $error['line']);
    }
}
function HandleError($message, $file, $line)
{
    // Prepare Error Data
    $error_data = 'File  : '. basename($file)              ."
".
                  'Line  : '. $line                        ."
".
                  'When  : '. date('l jS \of F Y h:i:s A') ."
".
                  'Error : '. $message;

    // Log Error To File
    $h = fopen('logs/'. date('d-m-Y') .'.log', 'a');
    fwrite($h, $error_data."
----------------------------------------------------------------------

");
    fclose($h);

    // Exit With Error
    header('Content-type: application/json');
    exit(json_encode(array(
        'IsError'  => true,
        'ErrorMsg' => $message .' | '. basename($file) .':'. $line,
        'Data'     => ''
    )));
}
set_error_handler("ErrorHandler");
set_exception_handler('ExceptionErrorHandler');
register_shutdown_function('ShutdownFunction');

To test this, on one of my files I called a non existing function called test() and this is the response that got produced:

<br />
<b>Fatal error</b>:  Call to undefined function test() in <b>/home/www-data/public_html/ipos_api/api_methods.php</b> on line <b>11</b><br />
{"IsError":true,"ErrorMsg":"Call to undefined function test() | api_methods.php:11","Data":""}

Why isn't the php error (just above my json response) being hidden? I've set the display_error to off.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dragon456101 2014-01-13 09:13
    关注

    Figured out what the problem was.

    I am using Nginx with PHP-FPM. On my php-fpm.d pool configuration, I had specified the display_errors on, therefore it was over-writing all settings (in-line using ini_set and php.ini itslef).

    After removing that config line and re-starting php-fpm, I was able to hide the error message and still handle it.

    评论

报告相同问题?

悬赏问题

  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂