If your application provides mainly api endpoints, you need to handle exceptions and transform them into json response. For this purpose, you should use common exception handlers provided by Common Bundle.
Each exception handler must implement ExceptionHandlerInterface.
Exceptions are then handled by ExceptionListener which listens on kernel.exception
event.
By default, error handling is enabled on all uri paths. You can change it by defining your own regexes for which should be the feature enabled:
anzu_systems_common:
errors:
only_uri_match:
- ^/api/
Default exception handler is used as fallback exception handler, by default is used DefaultExceptionHandler. You can change it in config:
anzu_systems_common:
errors:
default_exception_handler: App\Exception\Handler\YourDefaultExceptionHandler
By default, bellow listed exception handlers are used. If you want to use only some of them, specify exception_handlers
option and list handlers which you want to use:
anzu_systems_common:
errors:
exception_handlers:
- AnzuSystems\CommonBundle\Exception\Handler\NotFoundExceptionHandler
- AnzuSystems\CommonBundle\Exception\Handler\ValidationExceptionHandler
- AnzuSystems\CommonBundle\Exception\Handler\AppReadOnlyModeExceptionHandler
- AnzuSystems\CommonBundle\Exception\Handler\AccessDeniedExceptionHandler
- AnzuSystems\CommonBundle\Serializer\Exception\SerializerExceptionHandler
To register your own handler, just implement ExceptionHandlerInterface. If your application is using autoconfiguration, it will autoconfigure your service with tag anzu_systems_common.logs.exception_handler
and ExceptionListener
will use your own handler. In case you are not using autoconfiguration, tag your service on your own.