Symfony 4 — A good way to deal with exceptions for REST API

Jun 19, 2019 · 2 min read
Photo by Nate Grant on Unsplash

There are many ways to handle exceptions and put them in your response, especially if you are developing a REST API for your project.

In most cases, when you are creating an API interface, you could need to design a JSON response structure in order to use the same format for all clients, like the following:

"message": "",
"data": [],
"errors": {}

There are two types of errors: the simple message error that is usually given by the exception message and it will be display in the message key and the fields errors that is represented by a field error and it will be display in errors key. For instance in a form submitted response, errors represents the related input form errors, like the following:

"message": "Invalid form",
"data": {},
"errors": {
"email": [
"This is not a valid email"

This structure is most useful when we are dealing with an api. You can create a response like that simply creating an ApiResponse as the following:

It’s easy! But troubles come when we need to handle exceptions as a API response.

Handle exceptions

First of all lets create an EventListener to handle exceptions and change them for our purpose.

Now you need to register it as a service and tell to Symfony that it is a listener on kernel.exception by setting tag.

If you are a careful reader you could have noticed that $errors is an empty array. Which we need to do now is to transform fields errors, like form errors, into associative array. Here Normalizer component comes to help us to transform any data, in this case exceptions, into an array.

Normalize form exceptions

At this point we have to create a Normalizer for a form exception, but first lets create a FormException 😄

Now that we have a FormException lets create the related normalizer…

we need to register it as serializer.normalizer in service.yaml file

Normalizer factory

Like form exceptions we could have other custom field exceptions such as a violation list after validating an object, so we need to create a Factory to get the right Normalizer for the passed exception.

To pass registered normalizers to NormalizerFactory construct method Symfony provides a shortcut to inject all services tagged with a specific tag

Now all we need is to update the ExceptionListerner…

…and the game is done! 😃
We have finally handled exceptions and transform them to an API response!

I hope this article helps you to solve this issue.
If you solve it in other way or just you want to let me know what do you think about it, leave me a comment 😃

More From Medium

More from Ideneal

Related reads

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade