public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();

        if ($exception instanceof NotFoundHttpException) {

            $request = $event->getRequest();
            $registry = $this->container->get('ekyna_core.redirection.provider_registry');
            foreach ($registry->getProviders() as $provider) {
                if ($provider->supports($request) && false !== $response = $provider->redirect($request)) {
                    if ($response instanceof RedirectResponse) {
                        $event->setResponse($response);
                    } elseif (is_string($response) && 0 < strlen($response)) {
                        $response = $this->container
                            ->get('security.http_utils')
                            ->createRedirectResponse($request, $response, 301)
                        ;
                        $event->setResponse($response);
                    }
                    return;
                }
            }

        } elseif ($exception instanceof RedirectException) {

            // Check path
            $path = $exception->getPath();
            if (0 === strlen($path)) {
                return;
            }

            // Build the response
            $request = $event->getRequest();
            $response = $this->container
                ->get('security.http_utils')
                ->createRedirectResponse($request, $path)
            ;
            $event->setResponse($response);

            // Add flash
            if (0 < strlen($message = $exception->getMessage())) {
                $this->container
                    ->get('session')
                    ->getFlashBag()
                    ->add($exception->getMessageType(), $message)
                ;
            }

        } elseif ($exception instanceof HttpException) {

            // Don't send log about http exceptions.
            return;

        } elseif(!$this->container->getParameter('kernel.debug')) {

            $template = new TemplateReference('EkynaCoreBundle', 'Exception', 'exception', 'html', 'twig');
            $code = $exception->getCode();
            $email = $this->container->getParameter('error_report_mail');
            $request = $this->container->get('request_stack')->getMasterRequest();

            $content = $this->container->get('twig')->render(
                (string) $template,
                [
                    'status_code' => $code,
                    'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
                    'exception' => FlattenException::create($exception),
                    'request' => $request,
                    'logger' => null,
                    'currentContent' => null,
                ]
            );

            $report = \Swift_Message::newInstance('Error report', $content, 'text/html');
            $report->setFrom($email)->setTo($email);
            $this->container->get('mailer')->send($report);
        }
    }