Miles M.
Miles M.

Reputation: 4169

Custom error pages not working when exception controller is enabled

When implementing fos_rest bundle with symfony, I cannot seem to have Symfony's normal behavior when handling custom error pages on a 404, 405, 500 or any other error triggered by Symfony.

It works fine for every error triggered with the rest bundle in a normal rest controller.

But on my landing page (and about us and so on), which is not using fos_rest bundle, but twig instead, the custom error pages don't work, instead, it seems to be handled by the fos_rest bundle anyway, and always sends a default error 500 (even if it should be triggering a 404 error).

If I deactivate exceptions in fos_rest.yaml file (enabled: false), then, the custom error pages works fine (configured following this documentation here: https://symfony.com/doc/4.4/controller/error_pages.html )

fos_rest:
    routing_loader:
        default_format: json
        include_format: false
    body_listener: true
    format_listener:
        rules:
            - { path: '^/myROUTE1', priorities: ['json'], fallback_format: json, prefer_extension: false }
            - { path: '^/myROUTE2', priorities: ['json'], fallback_format: json, prefer_extension: false }
            - { path: '^/myROUTE3', priorities: ['json'], fallback_format: json, prefer_extension: false }
            - { path: '^/myROUTE4', priorities: ['json'], fallback_format: json, prefer_extension: false }
            - { path: '^/', priorities: ['html', 'json'], fallback_format: 'html' }
    param_fetcher_listener: true
    access_denied_listener:
        json: true
    view:
        view_response_listener: 'force'
        formats:
            json: true
    exception:
        enabled: true
        exception_controller: 'fos_rest.exception.controller:showAction'
        codes:
            Doctrine\ORM\EntityNotFoundException: 404
            \LogicException: 400
            \DomainException: 400
        messages:
            Doctrine\ORM\EntityNotFoundException: true
            \LogicException: true
            \DomainException: true

How do I set up fos_rest bundle to only handle exceptions for the routes handled by my rest controllers, and leave the normal Symfony 4 behavior for the rest of the site?

Upvotes: 0

Views: 622

Answers (1)

Miles M.
Miles M.

Reputation: 4169

I found a solution by using zones. I had to add the following piece of code in my fos_rest.yaml

zone:
    - { path: '^/myROUTE1' }
    - { path: '^/myROUTE2' }
    - { path: '^/myROUTE3' }
    - { path: '^/myROUTE4' }

As described here

Hope this helps.

Upvotes: 1

Related Questions