FOSRestBundle: Error handling with 2.8 beta
I’m trying to upgrade my project to FOSRestBundle 2.8 but can’t figure out how to handle errors correctly.
Previously we were relying on the ExceptionListener which is now deprecated. The call stack went like this:
/src/ApiBundle/Component/Serializer/JsonExceptionNormalizer.php:66
/src/ApiBundle/Component/Serializer/JsonExceptionNormalizer.php:45
/vendor/jms/serializer/src/GraphNavigator/SerializationGraphNavigator.php:192
/vendor/jms/serializer/src/Serializer.php:249
/vendor/jms/serializer/src/Serializer.php:162
/vendor/friendsofsymfony/rest-bundle/Serializer/JMSSerializerAdapter.php:62
/app/cache/test/ContainerS2qAfcv/JMSSerializerAdapter_a5dd94b.php:27
/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php:458
/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php:424
/vendor/friendsofsymfony/rest-bundle/View/ViewHandler.php:310
/vendor/friendsofsymfony/rest-bundle/Controller/ExceptionController.php:67
/vendor/symfony/http-kernel/HttpKernel.php:158
/vendor/symfony/http-kernel/HttpKernel.php:80
/vendor/symfony/http-kernel/EventListener/ErrorListener.php:60
/vendor/friendsofsymfony/rest-bundle/EventListener/ExceptionListener.php:81
In the JsonExceptionNormalizer I have some custom logic to add validation errors into the final json when the thrown exception contains symfony/validator violations.
What extension point can I use now when the ExceptionListener is deprecated? Or what can I use to trigger my current ViewHandler -> JMSSerializerAdapter -> JsonExceptionNormalizer logic?
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 20 (19 by maintainers)
Looks like I managed to fix it…
and updated my JsonExceptionNormalizer:
For the JMS Serializer we have the
FlattenExceptionHandler. You then need anErrorRenderInterfaceimplementation that is able to leverage the JMS serializer which you can register with theserializer_error_rendereroption:FosRestBundle did deprecate the ExceptionListener because Symfony has a new component called ErrorHandler which is used instead of that.
If you disable the exception listener you should make sure you disable also the deprecated ExceptionController of Symfony, FosRest should throw you an error if you forgot that here.
If you did correctly disable the deprecated symfony exception controller the following way:
The symfony error handler component should handle this correctly.
I’m not very familiar with implementation maybe @xabbuh can provide there more, whats best to extend it. In @sulu I did keep it simple and decorated the FosRest Error Normalizer to change the output:
https://github.com/sulu/sulu/pull/4798/files#diff-a097bf1329bf027e774b48ddb459fb5e https://github.com/sulu/sulu/pull/4798/files#diff-4f4ac11710f21c2c11567806aff5a1a5
Maybe this helps you.