title: Symfony5 - Overriding Error output for non-HTML formats
body: <p>I try handle all excpetions (Symfony 5) via official documentation: <a href="https://symfony.com/doc/current/controller/error_pages.html#overriding-error-output-for-non-html-formats" rel="nofollow noreferrer">https://symfony.com/doc/current/controller/error_pages.html#overriding-error-output-for-non-html-formats</a></p>
  <p>I want serialize all exception to JSON format.
  I read solution for similar problem in this topic: <a href="https://stackoverflow.com/questions/61126321/how-to-format-all-httpexceptions-as-json-in-symfony5">How to format all HttpExceptions as json in symfony5?</a>
  but doesn't resolve my problem.</p>
  <p>There my files: src/Controller/ShowController.php</p>
  <pre><code>&lt;?php
  
  namespace App\Controller;
  
  use App\Component\ShowResponseHandlerInterface;
  use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  use Symfony\Component\HttpFoundation\BinaryFileResponse;
  use Symfony\Component\HttpFoundation\Response;
  use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  use Symfony\Component\Routing\Annotation\Route;
  
  class ShowController extends AbstractController
  {
      /**
       * @Route(&quot;/show/{file}&quot;, name=&quot;show&quot;, requirements={&quot;file&quot;=&quot;.+&quot;})
       */
      public function index(string $file, string $storageDirectory): Response
      {
          $absolutePath = $storageDirectory.$file;
          if(!file_exists($absolutePath)) {
              throw new NotFoundHttpException(&quot;Resource not exists&quot;);
          }
  
          return new BinaryFileResponse($absolutePath);
  
      }
  }
  </code></pre>
  <p>src/Serializer/ProblemNormalizer.php</p>
  <pre><code>&lt;?php
  namespace App\Serializer;
  
  use Symfony\Component\ErrorHandler\Exception\FlattenException;
  use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  
  class ProblemNormalizer implements NormalizerInterface
  {
  
      public function normalize($exception, string $format = null, array $context = [])
      {
          return [
              'success' =&gt; false,
              'exception'=&gt; [
                  'message' =&gt; $exception-&gt;getMessage(),
                  'code' =&gt; $exception-&gt;getStatusCode(),
              ]
          ];
      }
  
      public function supportsNormalization($data, string $format = null)
      {
          return $data instanceof FlattenException;
      }
  }
  </code></pre>
  <p>config/services.yaml</p>
  <pre><code>services:
  _defaults:
      autowire: true      # Automatically injects dependencies in your services.
      autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
  
      App\Serializer\ProblemNormalizer:
          tags: ['serializer.normalizer']
  </code></pre>
  <p>and of course i have installed package symfony/serializer</p>
  <pre><code>composer show -i | grep &quot;symfony/serializer&quot;
  
  symfony/serializer                 v5.2.9  Handles serializing and deserializing data structures, including object graphs, into array structures or other formats like XML and JSON
  </code></pre>
  <p>What I'm doing wrong or what is not configured?
  It always return html output.</p>
  <p><strong>Update</strong></p>
  <p>Console output for:</p>
  <pre><code>symfony console debug:container | grep normalizer
    Symfony\Component\Serializer\Normalizer\DenormalizerInterface                alias for &quot;serializer&quot;                                                                     
    Symfony\Component\Serializer\Normalizer\ObjectNormalizer                     alias for &quot;serializer.normalizer.object&quot;                                                   
    Symfony\Component\Serializer\Normalizer\PropertyNormalizer                   alias for &quot;serializer.normalizer.property&quot;                                                 
    maker.auto_command.make_serializer_normalizer                                Symfony\Bundle\MakerBundle\Command\MakerCommand                                            
    maker.maker.make_serializer_normalizer                                       Symfony\Bundle\MakerBundle\Maker\MakeSerializerNormalizer                                  
    serializer.denormalizer.array                                                Symfony\Component\Serializer\Normalizer\ArrayDenormalizer                                  
    serializer.denormalizer.unwrapping                                           Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer                             
    serializer.normalizer.constraint_violation_list                              Symfony\Component\Serializer\Normalizer\ConstraintViolationListNormalizer                  
    serializer.normalizer.data_uri                                               Symfony\Component\Serializer\Normalizer\DataUriNormalizer                                  
    serializer.normalizer.dateinterval                                           Symfony\Component\Serializer\Normalizer\DateIntervalNormalizer                             
    serializer.normalizer.datetime                                               Symfony\Component\Serializer\Normalizer\DateTimeNormalizer                                 
    serializer.normalizer.datetimezone                                           Symfony\Component\Serializer\Normalizer\DateTimeZoneNormalizer                             
    serializer.normalizer.flatten_exception                                      Symfony\Component\Messenger\Transport\Serialization\Normalizer\FlattenExceptionNormalizer  
    serializer.normalizer.form_error                                             Symfony\Component\Serializer\Normalizer\FormErrorNormalizer                                
    serializer.normalizer.json_serializable                                      Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer                         
    serializer.normalizer.mime_message                                           Symfony\Component\Serializer\Normalizer\MimeMessageNormalizer                              
    serializer.normalizer.object                                                 Symfony\Component\Serializer\Normalizer\ObjectNormalizer                                   
    serializer.normalizer.problem                                                Symfony\Component\Serializer\Normalizer\ProblemNormalizer                                  
    serializer.normalizer.property                                               Symfony\Component\Serializer\Normalizer\PropertyNormalizer                                 
    serializer.normalizer.uid                                                    Symfony\Component\Serializer\Normalizer\UidNormalizer                                      
  
   // To search for a specific service, re-run this command with a search term. (e.g. debug:container log) 
  </code></pre>