Bolt\Controller\Exception::kernelException PHP Method

kernelException() public method

Route for kernel exception handling.
public kernelException ( GetResponseForExceptionEvent $event ) : Response
$event Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent
return Symfony\Component\HttpFoundation\Response
    public function kernelException(GetResponseForExceptionEvent $event)
    {
        if ($this->app === null) {
            throw new \RuntimeException('Exception controller being used outside of request cycle.');
        }
        $exception = $event->getException();
        $message = $exception->getMessage();
        if ($exception instanceof HttpExceptionInterface && !Zone::isBackend($event->getRequest())) {
            $message = "The page could not be found, and there is no 'notfound' set in 'config.yml'. Sorry about that.";
        }
        $context = $this->getContextArray($exception);
        $context['type'] = 'general';
        $context['message'] = $message;
        $html = $this->app['twig']->render('@bolt/exception/general.twig', $context);
        $response = new Response($html, Response::HTTP_OK);
        $response->headers->set('X-Debug-Exception-Handled', time());
        return $response;
    }

Usage Example

Example #1
0
 /**
  * Handle errors thrown in the application.
  *
  * @param GetResponseForExceptionEvent $event
  */
 public function onKernelException(GetResponseForExceptionEvent $event)
 {
     if ($this->isProfilerRequest($event->getRequest())) {
         return;
     }
     $exception = $event->getException();
     $message = $exception->getMessage();
     $statusCode = Response::HTTP_INTERNAL_SERVER_ERROR;
     if ($exception instanceof HttpExceptionInterface) {
         $statusCode = $exception->getStatusCode();
     }
     // Log the error message
     $level = LogLevel::CRITICAL;
     if ($exception instanceof HttpExceptionInterface && $exception->getStatusCode() < 500) {
         $level = LogLevel::WARNING;
     }
     $this->logger->log($level, $message, ['event' => 'exception', 'exception' => $exception]);
     // Get and send the response
     if ($this->isJsonRequest($event->getRequest())) {
         $response = new JsonResponse(['success' => false, 'errorType' => get_class($exception), 'code' => $statusCode, 'message' => $message]);
     } elseif ($this->config->get('general/debug_error_use_symfony')) {
         return null;
     } else {
         $response = $this->exceptionController->kernelException($event);
     }
     $response->setStatusCode($statusCode);
     $event->setResponse($response);
 }
All Usage Examples Of Bolt\Controller\Exception::kernelException