FriendsOfSymfony / FOSRestBundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony
http://symfony.com/doc/master/bundles/FOSRestBundle/index.html
MIT License
2.79k stars 703 forks source link

Exceptions mapped in config aren't caught anymore, 500 is thrown instead #2369

Open SebastienTediber opened 2 years ago

SebastienTediber commented 2 years ago

Hello,

Since we updated to 3.x.x, the exceptions I mapped in my config files aren't caught and a 500 is thrown instead.

Here is my configuration :

fos_rest:
    exception:
        enabled: true
        messages:
            Symfony\Component\HttpKernel\Exception\HttpException: true
            InvalidArgumentException: true
            Sylius\Component\Resource\Exception\UnexpectedTypeException: true
            PHPUnit\Framework\Warning: true

    view:
        formats:
            json: true
            xml:  true
        empty_content: 204
    format_listener:
        rules:
            - { path: '^/shop-api', priorities: ['json'], fallback_format: json, prefer_extension: true }
            - { path: '^/api/.*', priorities: ['json', 'xml'], fallback_format: json, prefer_extension: true }
            - { path: '^/cron/.*', priorities: ['json'], fallback_format: json, prefer_extension: false }
            - { path: '^/', stop: true }

I have been trying with InvalidArgumentException thrown.

I have been changing the configuration a few times, adding the code to my exception too, it still does not work.

GuillaumeTediber commented 2 years ago

Hello @goetas @W0rma

Do you have any ideas in how to use fos_rest error caching functionnality for version 3.X.X ? This would help us a lot.

Thank you,

Guillaume Bidan CTO Tediber

WissameMekhilef commented 2 years ago

Hi!

I have also been facing this issue, trying to clean up the log output to focus on what matter and having uncaught exception is making it harder.

InjustFr commented 2 years ago

@SebastienTediber I think your configuration needs an update. Mine is working fine, here it is (the exception part):

fos_rest:
    exception:
        map_exception_codes: true
        codes:
            'InvalidArgumentException': 422
            'Doctrine\ORM\EntityNotFoundException': 404
        messages:
            'InvalidArgumentException': true
            'Doctrine\ORM\EntityNotFoundException': true

You also have to set exception_controller to null in twig.yaml, if you're using twig

SebastienTediber commented 2 years ago

Hello @InjustFr,

We investigated the issue further and came upon a Twig issue too indeed, that we fixed differently. I will look into what you propose though because it looks better than our solution.

Thanks

SebastienTediber commented 2 years ago

Hello again @InjustFr ,

We tried your solution and it works fine for us.

Thanks again