Catch ErrorException包装一个致命的PHP错误 [英] Catch ErrorException that wraps a fatal PHP error

查看:248
本文介绍了Catch ErrorException包装一个致命的PHP错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我自制的PHP MVC框架中,我写了一个错误处理程序,将异常中的PHP错误包裹起来,然后将其抛出。

In my homemade PHP MVC framework, I've written a little error handler that wraps PHP errors in an exception, then throws it.

class ErrorController extends ControllerAbstract {

    ...

    public static function createErrorException($number, $message = NULL, $file = NULL, $line = NULL, array $context = array()) {
        throw new ErrorException($message, $number, 0, $file, $line);
    }
}

然后使用的set_error_han dler()。这是正常的,除了(不是双关语)的致命错误。我的自定义错误处理程序仍然被调用,但是我无法捕获抛出的 ErrorException

Which is then registered using set_error_handler(). This works fine, with the exception (no pun intended) of fatal errors. My custom error handler is still called, but I can't catch the ErrorException that is thrown.

这样的错误会试图包含一个不存在的文件:

An example of such an error would be trying to include a file that doesn't exist:

    try {
        require 'Controller/NonExistentController.php';
    } catch (ErrorException $exc) {
        echo $exc->getTraceAsString(); // code never reaches this block
    }

我的自定义错误处理程序被调用,异常被抛出,但代码从不到达catch块。相反,PHP生成HTML(不好!):

My custom error handler is called and the exception is thrown, but the code never reaches the "catch" block. Instead, PHP generates HTML (bad!):



警告:未捕获的异常'ErrorException'
消息'require(Controller / NonExistentController.php):无法打开流:...


Warning: Uncaught exception 'ErrorException' with message 'require(Controller/NonExistentController.php): failed to open stream: ...

后跟:


致命错误:Program :: main():Failed opening required
'Controller / NonExistentController .php'(include_path ='.:')in ...

Fatal error: Program::main(): Failed opening required 'Controller/NonExistentController.php' (include_path='.:') in ...

我不想尝试从致命错误中恢复,但我确实希望我的代码正常退出。在这种情况下,这意味着发送一个表示内部错误的XML或JSON响应,因为这是一个REST应用程序,这是我的客户期望的。 HTML响应很可能会破坏客户端应用程序。

I do not want to attempt recovering from a fatal error, but I do want my code to exit gracefully. In this instance, that means sending back an XML or JSON response indicating an internal error, because this is a REST application and that's what my clients expect. HTML responses would most likely break the client applications as well.

我应该这样做不同吗?

推荐答案

查看 要求 在php.net上:

Look at documentation about require on php.net:


需求是相同的到包括,除非失败,它也会产生致命的E_COMPILE_ERROR级错误。换句话说,它
将停止脚本,而只包含一个警告(E_WARNING)
,允许脚本继续。

require is identical to include except upon failure it will also produce a fatal E_COMPILE_ERROR level error. In other words, it will halt the script whereas include only emits a warning (E_WARNING) which allows the script to continue.

在您的情况下,您可以通过 register_shutdown_function 的帮助来处理致命错误,这需要PHP 5.2 +:

In your case you can handle fatal errors with help of register_shutdown_function, which requires PHP 5.2+:

function customFatalHandler() {
  $error = error_get_last();

  if( $error === NULL) {
      return;
  }

  $type   = $error["type"];
  $file = $error["file"];
  $line = $error["line"];
  $message  = $error["message"];

  echo "Error `$type` in file `$file` on line $line with message `$message`"; 
}

register_shutdown_function("customFatalHandler");

此外,这可以为您提供帮助

Also this can be helpfull for you


  1. error_get_last()和自定义错误处理程序

  2. 为什么'PHP找到一个找不到类错误?

  1. error_get_last() and custom error handler
  2. Why doesn't PHP catch a "Class not found" error?

这篇关于Catch ErrorException包装一个致命的PHP错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆