本文介绍了Symfony 2.4:为什么没有被kernel.exception侦听器捕获到500个错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个监听器,用于监听403,404和500个异常。这适用于403和404异常,但不适用于500个例外。对于500个异常(或异常,将返回为客户端的500个错误),从未调用onKernelException方法。在我当前的Symfony项目中,当代码被添加到一个干净的Symfony 2.4.1安装中时,似乎是一样的。

I'm trying to create a listener for listening 403, 404, and 500 exceptions. This works fine for 403 and 404 exceptions but not for 500 exceptions. For 500 exceptions (or exceptions that will be returned as 500 errors for the client) the method onKernelException is never called. 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.

然后我通过执行一个不存在的功能。

I then introduce a 500 error by executing a non-existent function.

在开发环境中,我收到一个Symfony生成的页面,说糟糕,看起来好像出了问题。然后查看有关抛出的UndefinedFunctionException的信息以及500状态代码。

In the development environment I get a Symfony generated page saying "Whoops, looks like something went wrong." and then views information about the thrown "UndefinedFunctionException" along with the 500 status code.

在生产环境中,我收到一个空页面以及500个状态代码。在错误日志prod.log中,我收到一个PHP致命错误:调用未定义的函数错误与堆栈跟踪。

In the production environment I get an empty page along with the 500 status code. In the error log prod.log I get a "PHP Fatal error: Call to undefined function" error with a stack trace.

由于Symfony显然捕获此错误,为什么我没有使用kernel.exception侦听器捕获相应的异常?

Since Symfony obviously catches this error, why can I not catch the corresponding exception with a kernel.exception listener?

我正在使用的类是:

<?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 }


推荐答案

PHP 7引入了致命错误异常。如果您在PHP 5.X中,应用程序将停止。

PHP 7 introduced fatal error exceptions. If you are in PHP 5.X the application stops.

这篇关于Symfony 2.4:为什么没有被kernel.exception侦听器捕获到500个错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 01:37