Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException PHP Method

onKernelException() public method

Handles security related exceptions.
public onKernelException ( GetResponseForExceptionEvent $event )
$event Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent An GetResponseForExceptionEvent instance
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        $exception = $event->getException();
        $request = $event->getRequest();

        // determine the actual cause for the exception
        while (null !== $previous = $exception->getPrevious()) {
            $exception = $previous;
        }

        if ($exception instanceof AuthenticationException) {
            if (null !== $this->logger) {
                $this->logger->info(sprintf('Authentication exception occurred; redirecting to authentication entry point (%s)', $exception->getMessage()));
            }

            try {
                $response = $this->startAuthentication($request, $exception);
            } catch (\Exception $e) {
                $event->setException($e);

                return;
            }
        } elseif ($exception instanceof AccessDeniedException) {
            $token = $this->context->getToken();
            if (!$this->authenticationTrustResolver->isFullFledged($token)) {
                if (null !== $this->logger) {
                    $this->logger->debug('Access denied (user is not fully authenticated); redirecting to authentication entry point');
                }

                try {
                    $response = $this->startAuthentication($request, new InsufficientAuthenticationException('Full authentication is required to access this resource.', $token, 0, $exception));
                } catch (\Exception $e) {
                    $event->setException($e);

                    return;
                }
            } else {
                if (null !== $this->logger) {
                    $this->logger->debug('Access is denied (and user is neither anonymous, nor remember-me)');
                }

                try {
                    if (null !== $this->accessDeniedHandler) {
                        $response = $this->accessDeniedHandler->handle($request, $exception);

                        if (!$response instanceof Response) {
                            return;
                        }
                    } elseif (null !== $this->errorPage) {
                        $subRequest = $this->httpUtils->createRequest($request, $this->errorPage);
                        $subRequest->attributes->set(SecurityContextInterface::ACCESS_DENIED_ERROR, $exception);

                        $response = $event->getKernel()->handle($subRequest, HttpKernelInterface::SUB_REQUEST, true);
                        $response->setStatusCode(403);
                    } else {
                        $event->setException(new AccessDeniedHttpException($exception->getMessage(), $exception));

                        return;
                    }
                } catch (\Exception $e) {
                    if (null !== $this->logger) {
                        $this->logger->err(sprintf('Exception thrown when handling an exception (%s: %s)', get_class($e), $e->getMessage()));
                    }

                    $event->setException(new \RuntimeException('Exception thrown when handling an exception.', 0, $e));

                    return;
                }
            }
        } else {
            return;
        }

        $event->setResponse($response);
    }

Usage Example

 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     $responseType = function () use($event) {
         $request = $event->getRequest();
         $accepts = $request->headers->get('accept', null);
         $contentType = $request->getContentType();
         return $accepts ? $accepts : $contentType;
     };
     if (strstr($responseType(), 'application/json') !== false) {
         $e = $event->getException();
         $response = new JsonResponse();
         $code = 500;
         if ($e instanceof NotFoundHttpException) {
             $code = 404;
         }
         if ($e instanceof AccessDeniedException) {
             $code = 403;
         }
         $response->setContent(json_encode(['error' => $e->getMessage(), 'code' => $code], true))->setStatusCode($code);
         $event->setResponse($response);
     } else {
         parent::onKernelException($event);
     }
 }