Symfony2 format exception thrown by method annotation

85 Views Asked by At

In Symfony 2.4 I'm using a route and method annotation like follows:

/** di elaborazione creazione offerta
 * 
 * @param Request $request
 * @return JsonResponse
 * 
 * @Route("/process", name="process", options={"expose" : true}, defaults={"_format" : "json"})
* @Method("POST")
*/

if I throw a MethodNotAllowedException inside action body, response is correctly a json formatted one whereas a call in HTTP: GET returns a fully formatted html exception page, as if _format attribute would not be loaded. Is it possible to pass _format attribute to ExceptionController sub-request?

1

There are 1 best solutions below

0
nurikabe On

Not allowing @Method("GET") means that Symfony rejects the request at the route level and executes the default exception controller. If you want to override the default exception output, override the default exception behavior as outlined here.

I think you can get a JSON exception simply by adding some .json.twig templates. You could also override the default exception controller if you need more flexibility.