Skip to content
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
Cannot retrieve contributors at this time

Step 4: ExceptionController support

To map Exception classes to HTTP response status codes an exception map may be configured, where the keys match a fully qualified class name and the values are either an integer HTTP response status code or a string matching a class constant of the Symfony\Component\HttpFoundation\Response class:

            'Symfony\Component\Routing\Exception\ResourceNotFoundException': 404
            'Doctrine\ORM\OptimisticLockException': HTTP_CONFLICT
            'Acme\HelloBundle\Exception\MyExceptionWithASafeMessage': true

If you want to display the message from the exception in the content of the response, add the exception to the messages map as well. If not only the status code will be returned.

If you know what status code you want to return you do not have to add a mapping, you can do this in your controller:


namespace AppBundle\Controller;

use Symfony\Component\HttpKernel\Exception\HttpException;

class UsersController extends Controller
    public function postUserCommentsAction($slug)
        if (!$this->validate($slug)) {
            throw new HttpException(400, "New comment is not valid.");

In order to make the serialization format of exceptions customizable it is possible to use serializer normalizers.

See how to create handlers for the JMS serializer and how to create normalizers for the Symfony serializer.

That was it!


If you are receiving a 500 error where you would expect a different response, the issue is likely caused by an exception inside the ExceptionController (for example the serializer failed). You should take a look at the logs of your app to see if an uncaught exception has been logged.