19/02/2016 - SYMFONY
You can catching a specific exception and throw a different one with different message or just change the message in symfony by using event listener. See example below.
We throw an exception if the order is for customer called inanzzz
or a car type audi
in controller.
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.');
}
}
If you send requests below to POST /order/create
, you should get Customer [inanzzz] is forbidden. 400 Bad Request - BadRequestHttpException
or Car [audi] is not available. 500 Internal Server Error - InvalidOptionsException
exception.
# Throws BadRequestHttpException
{
"customer_name": "inanzzz",
"car_make": "bmw",
"car_model": "3.16"
}
# Throws InvalidOptionsException
{
"customer_name": "hello",
"car_make": "audi",
"car_model": "a3"
}
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!');
}
}
}
services:
application_frontend.event_listener.order_create:
class: Application\FrontendBundle\EventListener\OrderCreateListener
tags:
- { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
Messages will change as shown below.
# 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