Even listener ile symfony içinde exception yakalayıp, yeni bir exception yaratma veya sadece mesajını değiştirme işlemi yapabilirsiniz.


Controller


Eğer siparişin sahibi inanzzz ise veya audi içinse, controller ile exception oluşturuyoruz.


namespace Application\FrontendBundle\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

/**
* @Route("order", service="application_frontend.controller.order")
*/
class OrderController
{
/**
* @param Request $request
*
* @Method({"POST"})
* @Route("/create")
*
* @return Response
* @throws BadRequestHttpException|InvalidOptionsException
*/
public function createAction(Request $request)
{
$order = json_decode($request->getContent(), true);

if ($order['customer_name'] == 'inanzzz') {
throw new BadRequestHttpException('Customer [inanzzz] is forbidden.');
}

if ($order['car_make'] == 'audi') {
throw new InvalidOptionsException('Car [audi] is not available.');
}

return new Response('Valid order.');
}
}

Mevcut exception


Eğer aşağıdaki istekleri POST order/create adresine gönderirseniz, ya Customer [inanzzz] is forbidden. 400 Bad Request - BadRequestHttpException ya da Car [audi] is not available. 500 Internal Server Error - InvalidOptionsException exception alırsınız.


# Throws BadRequestHttpException
{
"customer_name": "inanzzz",
"car_make": "bmw",
"car_model": "3.16"
}

# Throws InvalidOptionsException
{
"customer_name": "hello",
"car_make": "audi",
"car_model": "a3"
}

Exception değiştirme


OrderCreateListener


namespace Application\FrontendBundle\EventListener;

use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;

class OrderCreateListener
{
public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();

if ($exception instanceof BadRequestHttpException) {
$event->setException(new BadRequestHttpException('I told you not to come back!'));
}

if ($exception instanceof InvalidOptionsException) {
throw new NotFoundHttpException('Buy a different car!');
}
}
}

Listeners.yml


services:
application_frontend.event_listener.order_create:
class: Application\FrontendBundle\EventListener\OrderCreateListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

Sonuç


Yeni exception mesajları aşağıdaki gibi olacak.


# Original => Overridden

Customer [inanzzz] is forbidden. 400 Bad Request - BadRequestHttpException => I told you not to come back!. 400 Bad Request - BadRequestHttpException
Car [audi] is not available. 500 Internal Server Error - InvalidOptionsException => Buy a different car! 404 Not Found - NotFoundHttpException