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:
            DoctrineORMEntityNotFoundException: 404
            LogicException: 400
            DomainException: 400
        messages:
            DoctrineORMEntityNotFoundException: 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?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *