I'm struggling with a problem linked to the FOSRestBundle (version 0.13.*)
I have some REST api that throws some exceptions, nothing unusual I guess. But, despite the specific configuration I made to allow exceptions messages to be formatted in the response even in production (following the documentation I found here : https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/4-exception-controller-support.md), the JSON response stays desperately empty...
Example below:
http://host/app_dev.php/api/postcode/search?postcode=
results in:
HTTP 400: {"status":"error","status_code":400,"status_text":"Bad Request","current_content":"","message":"You must provide a postcode"}
BUT
http://host/api/postcode/search?postcode=
results in:
HTTP 400: []
My API controller looks like this:
/**
* Search post codes
*
* @param Request $request Request
* @param Promotion $promotion Promotion
*
* @Rest\View()
*
* @throws BadRequestHttpException
* @return array
*/
public function searchAction(Request $request, Promotion $promotion)
{
// Get post code
$postCode = $request->query->get('postcode');
if (!$postCode) {
throw new BadRequestHttpException('You must provide a postcode');
}
// SOME LOGIC HERE TO GET DATA
return $data;
}
and the fos_rest configuration looks like this:
fos_rest:
routing_loader:
default_format: json
view:
mime_types:
json: ['application/json; charset=UTF-8']
formats:
json: true
view_response_listener: force
format_listener: false
access_denied_listener:
json: true
body_listener: true
exception:
messages:
Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
As I understood it, the fos_rest.exception.messages configuration array should list the exceptions for which I want a serialization of the error message even in production. As you can see in the code of the controller, this response contains a translated error message that will be displayed to the client. Why is this configuration ignored? I can say for sure that the configuration is properly loaded even in prod environment, because if I mispell the class name in the conf, it fails with a "Could not load class" exception.
What am I missing? Thanks in advance for any hint you could give me...