Skip to content

Change HTML errors to JSON #42441

Discussion options

You must be logged in to vote

You need to install serializer in order for Symfony's json error handler to kick in.

See https://symfony.com/doc/current/controller/error_pages.html#overriding-error-output-for-non-html-formats

Other alternative would be to create an exception handler which would set response to json.

As an example.

class JSONExceptionListener implements EventSubscriberInterface
{
    /**
     * @var string
     */
    private $environment;

    public function __construct(string $environment)
    {
        $this->environment = $environment;
    }

    public static function getSubscribedEvents()
    {
        return [
            KernelEvents::EXCEPTION => ['onKernelException', 200],
        ];
    }

    

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
1 reply
@dbrumann
Comment options

Answer selected by alexanderkatkov
Comment options

You must be logged in to vote
1 reply
@alexanderkatkov
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants