Symfony events: kernel.exception reference
20 November 2022 (Updated 20 November 2022)
When is it dispatched?
Dispatched as soon as an error occurs during the handling of an HTTP request (i.e., in HttpKernel::handle
). Internally, the body of the handle()
method is wrapped in a try-catch block.
When an exception is thrown, the kernel.exception
event is dispatched so that your system can deal with the exception however it needs to. See here for the relevant source code.
Use cases
- Recover from certain types of exceptions and return an appropriate response.
- Modifying the exception details sent in the response.
Example
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ExceptionEvent;
public function onKernelException(ExceptionEvent $event)
{
$exception = $event->getThrowable();
$response = new Response();
// setup the Response object based on the caught exception
$event->setResponse($response);
// you can alternatively set a new Exception
// $exception = new \Exception('Some special exception');
// $event->setThrowable($exception);
}
List event listeners
php bin/console debug:event-dispatcher kernel.exception
- Handling Exceptions: the
kernel.exception
Event - Symfony kernel events reference
- Symfony events: kernel.request reference
- Symfony events: kernel.controller reference
- Symfony events: kernel.controller_arguments reference
- Symfony events: kernel.view reference
- Symfony events: kernel.response reference
- Symfony events: kernel.finish_request reference
- Symfony events: kernel.terminate reference
- Symfony events: kernel.exception reference
Tagged:
Symfony
Thanks for your comment 🙏. Once it's approved, it will appear here.
Leave a comment