A
A
Alexey Verkhovtsev2019-04-17 00:17:20
symfony
Alexey Verkhovtsev, 2019-04-17 00:17:20

How to properly handle my exception not found symfony 4?

How to properly handle your exception? If I do something like this

public function onKernelException(GetResponseForExceptionEvent $event): void
    {
        $exception = $event->getException();
        $response = $event->getResponse();
        //dd($exception, $response);
        if ($exception instanceof NotFoundException) {
            $event->setResponse(new Response($exception->getUserMessage(), $exception->getCode()));
        }
    }

then I just get a 404 in response and display my text, but it does not look like a 404 error and symphony and I suspect the 404 error template will not connect in this case either.
Can you do it like this
public function onKernelException(GetResponseForExceptionEvent $event): void
    {
        $exception = $event->getException();
        $response = $event->getResponse();
        //dd($exception, $response);
        if ($exception instanceof NotFoundException) {
            $event->setException(new NotFoundHttpException($exception->getUserMessage()));
        }
    }

but it seems to me that this is not true.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question