Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FOSRestBundle configuration of exceptions messages in prod environment

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...

like image 962
Guillaume PETIT Avatar asked Oct 14 '14 12:10

Guillaume PETIT


1 Answers

I had a similar problem, your question helped me solve it actually! I know it is late but I found that clearing the prod cache helped fixed this for me.

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 wondering also whether you always have to explicitly add the exceptions to the codes and messages sections of the config, unless you use the HttpException:

throw new HttpException(400, "New comment is not valid.");
like image 55
timhc22 Avatar answered Oct 19 '22 03:10

timhc22