I\'m using FOSRestBundle with Symfony 2 to implement a REST API in JSON format.
I want all API exceptions to be returned in a specific JSON format like this:
Note: This works only for FOSResBundle < 2.0. For FOSResBundle >= 2.0 please use Exception Normalizers, see examples.
You can write custom exception wrapper handler like in docs. In your case:
false,
'exception' => array(
'exceptionClass' => $exception->getClass(),
'message' => $data['status_text']
)
);
return $newException;
}
}
fos_rest:
routing_loader:
default_format: json
view:
view_response_listener: force
exception_wrapper_handler: AppBundle\Handler\MyExceptionWrapperHandler
exception:
enabled: true
Response example:
{"success":false,"exception":{"exceptionClass":"Symfony\\Component\\HttpKernel\\Exception\\NotFoundHttpException","message":"Not Found"}}