【问题标题】:Catch Symfony 2 exceptions during the boot sequence在引导过程中捕获 Symfony 2 异常
【发布时间】:2016-12-20 17:33:14
【问题描述】:

我在 Symfony 文档中了解到内核事件:http://symfony.com/doc/current/components/http_kernel.html

上面写着:As you've seen, you can create and attach event listeners to any of the events dispatched during the HttpKernel::handle() cycle

没问题,我可以创建一个自定义异常监听器并监听内核事件。

但是我怎样才能在启动过程中发现潜在的错误(因为没有调用任何监听器):

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
{
    if (false === $this->booted) {
        $this->boot(); // Error can be thrown
    }

    return $this->getHttpKernel()->handle($request, $type, $catch);
}

例如,如果我提供了对数据库 (DriverException) 的错误访问权限,我可能会遇到这种错误。

在开发模式下,这没关系,因为我有一个默认错误处理程序 DebugBundle,但在生产中它是一个白屏。

我该如何正确处理?

【问题讨论】:

  • 您可以在 web/app.php 中的代码周围放置一个 try/catch 块。不完全确定您可以对任何捕获的异常做什么,但至少您可以捕获它们

标签: php symfony exception events kernel


【解决方案1】:

无需在应用程序本身的启动阶段捕获错误。

一般应用程序无法拦截所有可能的错误。例如,内存不足。或者 PHP 可能因段错误而崩溃,try/catchregister_shutdown_function 都无济于事。

最常见的解决方案是由 Web 服务器进行错误处理(NginxApache 等)。如果上游下降,那么 Web 服务器会收到 500 响应并通过显示用户友好的消息来很好地处理它。

【讨论】:

  • 是的,这很好,但是如果我使用 fastcgi_intercept_errors 设置 nginx error_page,之后我的所有错误都由 nginx 处理(甚至是我想在 Symfony 中捕获的错误)。
  • 您可以创建自定义 ExceptionListener 来设置其他 HTTP 代码。如果错误是可捕获和可恢复的,则它不是 500。根据设计:“500 内部服务器错误:服务器遇到了一个 unexpected 条件,导致它无法满足请求”(RFC2616)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多