4

I'm trying to add a custom control of exceptions in FOSRestBundle but it seems to ignore my custom messages (the status code of the response is ok).

I have:

throw new HttpException(404, "User {$id} not found");

But get this json response:

{
  "error": {
    "code": 404,
    "message": "Not Found"
  }
}

So I don't find the way to show my custom message

slashsbin
  • 326
  • 3
  • 8
K. Weber
  • 2,643
  • 5
  • 45
  • 77

4 Answers4

4

On the View Layer documentation: If you don't like the default exception structure, you can provide your own implementation.

class ExceptionWrapperHandler implements ExceptionWrapperHandlerInterface
{
    /**
     * @param array $data
     *
     * @return array
     */
    public function wrap($data)
    {
        // we get the original exception
        $exception = $data['exception'];

        // some operations
        // ...

        // return the array
        return array(
            'code'    => $code,
            'message' => $message,
            'value'   => $value
        );
    }
}


// config.yml
fos_rest:
    view:
        exception_wrapper_handler: Namespace\To\ExceptionWrapperHandler
AlixB
  • 1,208
  • 2
  • 10
  • 19
  • It's not working, I'm not working with forms or views, I return a JsonResponse with the data always. Maybe this solution doesn't work without views? – K. Weber Jul 29 '14 at 09:25
  • This is something related to the views, indeed. Why aren't you using views ? This does not suit your needs ? Because FOSRestBundle process JSON response as a view. – AlixB Jul 29 '14 at 09:29
  • My api is only json so I work with input data as simple parameters (not `FormType`) and return always a `return new JsonResponse($array_data);`, when an exception occurs I `throw new HttpException(404, "User {$id} not found");` I was in a hurry to deploy so made it more straight – K. Weber Jul 29 '14 at 09:32
  • 1
    You can return only JSON if you want with FOSRest. You should take a look to the documentation (and the view layer). It can do what you want :). But anyway, this solution should work, FOSRest use this to process exceptions. – AlixB Jul 29 '14 at 09:35
  • Does not do anything =( maybe this feature has been removed but documentation hasn't been updated? – hsb1007 Apr 21 '15 at 12:55
  • Turns out it is [only possible to do if you are using the JMS Serializer Bundle](http://symfony.com/doc/current/bundles/FOSRestBundle/4-exception-controller-support.html). – bassplayer7 Jul 21 '15 at 16:50
4

You could also throw you own exception using the following configuration:

fos_rest:
    exception:
        codes:
            'My\Custom\NotFound\Exception404': 404
        messages:
            'My\Custom\NotFound\Exception404': true

Provide a custom message in your own exception class and it should work as expected.

piotr.jura
  • 810
  • 9
  • 17
  • I tried this and still get the default http message for exceptions in prod environment (such as Bad Request for a 400), both with my custom exception class or with Symfony's. It works without problem in dev. Did I miss any other configuration to do? – Jukurrpa Nov 24 '14 at 19:03
  • 1
    Try setting exception_controller under twig section in config.yml: 'FOS\RestBundle\Controller\ExceptionController::showAction' – piotr.jura Nov 24 '14 at 20:43
  • Thanks, setting this option in the `twig` section did it. For some reason the documentation says it can be set in the `fos_rest.exception` section, but it doesn't actually accept it. – Jukurrpa Nov 27 '14 at 10:36
  • Personally, i add to remove var/cache directory to make it works ! Thanks ! – pcmanprogrammeur Apr 15 '20 at 10:31
4

To display your custom exception message (simply into app/config/config.yml):

fos_rest:
    exception:
         messages:
            Symfony\Component\HttpKernel\Exception\HttpException: true
Tanguy Bernard
  • 274
  • 5
  • 15
0

Another simple solution is, to set the debug flag in the fos_rest config like this:

fos_rest:
    // ...
    exception:
        // ...
        debug: true

This will show the actual exception message for all exceptions. Obviously this could be a security risk, so use it with caution.

Tobias Xy
  • 2,039
  • 18
  • 19