简体   繁体   English

Symfony 2.4:为什么kernel.exception监听器没有捕获到500个错误

[英]Symfony 2.4: Why are 500 errors not caught by kernel.exception listener

I'm trying to create a listener for listening 403, 404, and 500 exceptions. 我正在尝试创建一个侦听器,用于侦听403,404和500个异常。 This works fine for 403 and 404 exceptions but not for 500 exceptions. 这适用于403和404例外但不适用于500例外。 For 500 exceptions (or exceptions that will be returned as 500 errors for the client) the method onKernelException is never called. 对于500个异常(或将作为客户端的500个错误返回的异常),永远不会调用onKernelException方法。 It appears to be the same in my current Symfony project and when the code is added to a clean Symfony 2.4.1 install. 它在我当前的Symfony项目中似乎是相同的,并且代码被添加到干净的Symfony 2.4.1安装中。

I then introduce a 500 error by executing a non-existent function. 然后我通过执行不存在的函数引入500错误。

In the development environment I get a Symfony generated page saying "Whoops, looks like something went wrong." 在开发环境中,我得到一个Symfony生成的页面,上面写着“哎呀,看起来像是出了问题。” and then views information about the thrown "UndefinedFunctionException" along with the 500 status code. 然后查看有关抛出的“UndefinedFunctionException”以及500状态代码的信息。

In the production environment I get an empty page along with the 500 status code. 在生产环境中,我得到一个空页面以及500状态代码。 In the error log prod.log I get a "PHP Fatal error: Call to undefined function" error with a stack trace. 在错误日志prod.log中,我得到一个带有堆栈跟踪的“PHP致命错误:调用未定义函数”错误。

Since Symfony obviously catches this error, why can I not catch the corresponding exception with a kernel.exception listener? 既然Symfony明显捕获了这个错误,为什么我不能用kernel.exception监听器捕获相应的异常呢?

The class I'm using is: 我正在使用的课程是:

<?php

namespace SystemBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;

/**
 * This exception listener will listen to 500, 404, and 403 errors and render a corresponding view
 *
 * @SuppressWarnings("static")
 * @SuppressWarnings("else")
 */
class ExceptionListener
{
    protected $templating;
    protected $kernel;

    public function __construct(EngineInterface $templating, $kernel)
    {
        $this->templating = $templating;
        $this->kernel = $kernel;
    }

    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $container = $this->kernel->getContainer();

        // Exception object
        $exception = $event->getException();

        // Create Response object
        $response = new Response();

        // Get view name
        $viewName = $container->getParameter('theme') . ':Exception:exception.html.twig';
        if (!$this->templating->exists($viewName)) {
            $viewName = 'AckebrinkChallengerSystemBundle:Exception:exception.html.twig';
        }

        // Set response content
        $response->setContent($this->templating->render($viewName, array('exception' => $exception)));

        // HttpExceptionInterface is a special type of exception that
        // holds status code and header details
        if ($exception instanceof HttpExceptionInterface) {
            $response->setStatusCode($exception->getStatusCode());
            $response->headers->replace($exception->getHeaders());
        } else {
            $response->setStatusCode(500);
        }

        // set the new $response object to the $event
        $event->setResponse($response);
    }
}

and the service configuration I use is: 我使用的服务配置是:

services:
    kernel.listener.system_exception_listener:
        class: SystemBundle\Listener\ExceptionListener
        arguments:
            - @templating
            - @kernel
        tags:
            - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

First of all, ensure you cleared the Prod cache. 首先,确保清除Prod缓存。

Second, see if something in the web server is catching errors for you, ie fastcgi_intercept_errors in Nginx, but this is unlikely to happen. 其次,看看Web服务器中的某些东西是否正在为您捕获错误,即Nginx中的fastcgi_intercept_errors,但这不太可能发生。

Third, try throwing an exception rather than calling a non-declared function. 第三,尝试抛出异常而不是调用非声明的函数。

Fourth, try to $event->setResponse as early as possible in the exception listener to ensure there's no error in the handler itself. 第四,在异常监听器中尽可能早地尝试$ event-> setResponse,以确保处理程序本身没有错误。

Other than that, I have no idea. 除此之外,我不知道。 The code seems to be Ok. 代码似乎没问题。 Have you tried with XDebug to see how the code flows? 您是否尝试过使用XDebug来查看代码是如何流动的?

PHP 7 introduced fatal error exceptions. PHP 7引入了致命错误异常。 If you are in PHP 5.X the application stops. 如果您使用的是PHP 5.X,则应用程序将停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM