FOSRestBundle prod exception message configuration
I am struggling with a FOSRestBundle related issue (version 0.13. *)
I have some REST api that throws some exceptions, nothing out of the ordinary I guess. But despite some specific configuration I did to allow formatting of exception messages even during production (following the documentation I found here: https://github.com/FriendsOfSymfony/FOSRestBundle/blob/master/Resources/doc/ 4-exception-controller-support.md ) the JSON response remains desperately empty ...
See example below:
http://host/app_dev.php/api/postcode/search?postcode=
leads to:
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=
leads to:
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 config 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, the fos_rest.exception.messages config array should list the exceptions for which I want to serialize the error message even in production. As you can see in the controller code, this response contains a translated error message that will be displayed to the client. Why is this configuration being ignored? I can confidently say that the config is properly loaded even in the prod environment, because if I misuse the class name in the conf, it fails with the "Cannot load class" exception.
What am I missing? Thanks in advance for any hint you could give me ...
source to share
I had a similar problem, your question helped me solve it actually! I know it's late, but I found that clearing the cache prod
helped me fix this.
Also these are my settings:
fos_rest:
param_fetcher_listener: true
body_listener:
array_normalizer: fos_rest.normalizer.camel_keys
format_listener: true
view:
view_response_listener: 'force'
formats:
json: true
templating_formats:
html: true
force_redirects:
html: true
failed_validation: HTTP_BAD_REQUEST
default_engine: twig
routing_loader:
default_format: json
serializer:
serialize_null: true
access_denied_listener:
json: true
exception:
enabled: true
messages:
Symfony\Component\HttpKernel\Exception\BadRequestHttpException: true
I'm also wondering if you should explicitly add exceptions to sections codes
and messages
to config if you don't use HttpException:
throw new HttpException(400, "New comment is not valid.");
source to share