Symfony2内核异常事件未在生产模式下处理致命错误异常

Symfony2内核异常事件未在生产模式下处理致命错误异常,symfony,exception-handling,symfony-2.5,Symfony,Exception Handling,Symfony 2.5,我为异常处理创建了一个侦听器。下面是我的代码 服务.yml kernel.listener.prod_exception_listener: class: MyBundle\Listener\ExceptionListener tags: - { name: kernel.event_listener, event: kernel.exception, method: onKernelException } <?php namespace MyBundle

我为异常处理创建了一个侦听器。下面是我的代码

服务.yml

kernel.listener.prod_exception_listener:
    class: MyBundle\Listener\ExceptionListener
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
<?php
namespace MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionListener
{
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // no fatal exception goes here others are coming in this function
        // like 403,404,500 are coming in this block

    }
}
例外Listener.php

kernel.listener.prod_exception_listener:
    class: MyBundle\Listener\ExceptionListener
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
<?php
namespace MyBundle\Listener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionListener
{
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        // no fatal exception goes here others are coming in this function
        // like 403,404,500 are coming in this block

    }
}

我用下面的方法解决了它,
为我效劳

api_exception_subscriber:
    class: AppBundle\EventListener\ApiExceptionSubscriber
    arguments: ['%kernel.debug%', '@api.response_factory', '@logger']
    tags:
        - { name: kernel.event_subscriber }
api.response_factory:
    class: AppBundle\Api\ResponseFactory
我的反应工厂看起来像:

<?php

namespace AppBundle\Api;

use Symfony\Component\HttpFoundation\JsonResponse;

class ResponseFactory
{
    public function createResponse(ApiProblem $apiProblem)
    {
        $data = $apiProblem->toArray();

        $response = new JsonResponse(
            $data,
            $apiProblem->getStatusCode()
        );
        $response->headers->set('Content-Type', 'application/json');

        return $response;
    }
} 

编辑2020:自Symfony 5起,不再需要此功能

我通过重写Kernel::handle手动调用ExceptionListener来处理这个问题

public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true): Response
{
    try {
        return parent::handle($request, $type, $catch);
    } catch (\Exception $exception) {
        throw new \Exception("There was an issue booting the framework");
    } catch (\Throwable $throwable) {
        $exception = new FatalThrowableError($throwable);

        $event = new ExceptionEvent($this, $request, $type, $exception);
        /** @var ExceptionListener $exceptionListener */
        $exceptionListener = $this->container->get(ExceptionListener::class);
        $exceptionListener->onKernelException($event);

        return $event->getResponse();
    }
}