【问题标题】:Can I disable error/exception handling in Silex?我可以在 Silex 中禁用错误​​/异常处理吗?
【发布时间】:2016-07-30 11:19:06
【问题描述】:

我正在构建一个基于 Silex 1.3 的应用程序。这是我第一次接触Silex,所以对它不是很熟悉。

我想使用我自己的错误/异常处理程序,它基本上是一个注册自己的类,然后将捕获所有错误、致命错误和未捕获的异常并处理它们,无论是使用开发中的 Whoops 还是优雅生产中的处理程序。

但是,一旦我进入了一个 silex 控制器、中间件等,Silex 将接管并使用它自己的错误处理。我的仍然会捕获致命错误,因为 Silex 显然不会挂接到关机,但其他所有内容都替换为来自 Silex 的默认“出现问题”页面。

我知道我可以使用 $app->error() 来覆盖 Silex 处理错误的方式,但我还没有找到一种方法可以从那里将事物设置回原来的 ErrorHandler,或者覆盖 WHETHER Silex 处理错误.

那么,有谁知道如何 a) 告诉 Silex 使用我的错误处理程序,使用 $app->error() 或其他方式,b) 完全禁用 Silex 中的错误处理,或 c)作为最后的手段,让 Silex 捕获致命错误,以便我可以在 $app->error() 中处理所有三种类型?

由于这是我第一次使用 Silex,如果有更好的方法,请随时纠正我或告诉我您如何处理 Silex 中的错误,但如果可以的话,也请回答问题。

一些示例代码:

// This will register itself and then handle all errors.
$handler = new ErrorHandler();

// These are all handled appropriately.
nonexistentfunction();            // Correctly caught by ErrorHandler::handleFatalError
trigger_error("example");         // Correctly caught by ErrorHandler::handlePhpError
throw new \Exception("example");  // Correctly caught by ErrorHandler::handleException

$app = new \Silex\Application();
$app->get('/', function () use ($app) {

    // This is still handled correctly.
    nonexistentfunction();            // Correctly caught by ErrorHandler::handleFatalError

    // However, these are now overridden by Silex.
    trigger_error("example");         // INCORRECTLY DISPLAYS SILEX ERROR PAGE.
    throw new \Exception("example");  // INCORRECTLY DISPLAYS SILEX ERROR PAGE.

});
$app->run();

还有一个非常简化的ErrorHandler供参考:

Class ErrorHandler
{
    public function __construct()
    {
        $this->register();
    }

    private function register()
    {
        register_shutdown_function( array($this, "handleFatalError") );
        set_error_handler(array($this, "handlePhpError"));
        set_exception_handler(array($this, "handleException"));
    }

    // Etc.

}

【问题讨论】:

    标签: php symfony error-handling frameworks silex


    【解决方案1】:

    请注意 ExceptionHandler::disable() 一直是deprecated in 1.3 and removed in 2.0。所以:

    在 Silex 2.0 之前:

    $app['exception_handler']->disable();
    

    在 Silex 2.0+ 中:

    unset($app['exception_handler']);
    

    【讨论】:

      【解决方案2】:

      Silex doc

      看起来,您还需要注册一个 ExceptionHandler。 Silex 会将致命错误转化为异常以进行处理。另外,如果我没记错的话,这种异常会在控制器和中间件(至少在中间件之前)被“抛出”时被捕获,而不是在模型内。

      最后,您可以添加以下内容来处理事情。

      // register generic error handler (this will catch all exceptions)
      $app->error(function (\Exception $e, $exceptionCode) use ($app) {
          //if ($app['debug']) {
          //    return;
          //}
      
          return \Service\SomeHelper::someExceptionResponse($app, $e);
      });
      
      return $app;
      

      希望对你有所帮助。

      【讨论】:

        【解决方案3】:

        我知道(b) 选项,您可以完全禁用 Silex 应用程序错误处理程序,之后,您的自定义错误处理程序应该可以按照您的定义正常工作。

        完全禁用 Silex 错误处理程序:

        $app['exception_handler']->disable();
        

        所以,它会是这样的:

        require_once  'Exception.php'; # Load the class
        $handler = new ErrorHandler(); # Initialize/Register it
        
        $app = new \Silex\Application();
        $app->get('/', function () use ($app) {
        
        
            nonexistentfunction();  
            trigger_error("example");
            throw new \Exception("example");
        
        });
        $app->run();
        

        【讨论】:

        • $app['exception_handler']->disable();谢谢!这正是我所需要的。有趣的是,现在你把它给了我,我可以在 API 中找到它。我希望他们的文档更好一点 =o)
        【解决方案4】:

        您必须在您的应用中注册特定的提供商: https://github.com/whoops-php/silex-1

        【讨论】:

        • 老兄,说真的。你甚至读过这个问题吗?通过阅读问题,我的意思是甚至只是阅读问题的标题?甚至只是标题。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2010-10-07
        • 1970-01-01
        • 2012-10-30
        • 1970-01-01
        • 2021-12-04
        • 2019-09-30
        • 1970-01-01
        相关资源
        最近更新 更多