/** * Handle exceptions that occur during the request handling process. * * @param \Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event * * @throws \Exception|\Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException|\Symfony\Component\HttpKernel\Exception\HttpException * As a fallback behaviour only, if a sensible error page cannot be generated. */ public function onKernelException(GetResponseForExceptionEvent $event) { LoggerRegistry::debug('EngineExceptionListener performing handleException() on EXCEPTION event'); $exception = $event->getException(); try { // Delete any buffered page content. while (ob_get_level() > 1) { ob_end_clean(); } // Determine the status code to use for the error page (and which error page is displayed). $statusCode = $exception instanceof HttpException ? $exception->getStatusCode() : ($exception instanceof FileNotFoundException ? 404 : 500); // Set route and template attributes to error page defaults. $event->getRequest()->attributes->add(array('_status' => $statusCode, '_route' => $this->getEngine()->getErrorRoute($statusCode), '_route_params' => array('exception' => $exception, 'statusCode' => $statusCode), '_template' => $this->getEngine()->getErrorTemplate())); // Store the exception and status code in the top-level view data. $page = $this->getEngine()->getViewFactory()->getPage(); $page['exception'] = $exception; $page['status-code'] = $statusCode; // Perform standard rendering using a proxy event. $renderer = new EngineRendererListener($this->getEngine()); $viewScript = sprintf('error-%3d', $statusCode); // TODO Handle view scripts that don't exist, default back to 500 or 404 $renderEvent = new GetResponseForControllerResultEvent($event->getKernel(), $event->getRequest(), HttpKernelInterface::SUB_REQUEST, $viewScript); $renderer->onKernelView($renderEvent); // Copy the response from the proxy event back to the event being handled. $event->setResponse($renderEvent->getResponse()); } catch (\Exception $e) { try { // Delete any buffered page content. while (ob_get_level() > 1) { ob_end_clean(); } // If there is an error above, try to display the fallback error page, i.e. black-and-white error // message. $event->setResponse(Response::create(HtmlUtilities::exception($exception, $this->getEngine()->getSiteInfo()->getAdministratorName(), $this->getEngine()->getSiteInfo()->getAdministratorEmail()))); } catch (\Exception $e2) { // If another error occurs, the best thing to do is throw the original error. throw $exception; } } }
public function testExceptionWithAdminDetails() { $e = new \Exception('ERROR MESSAGE'); $this->assertStringMatchesFormat("%AName: Your Administrator%A", HtmlUtilities::exception($e, 'Your Administrator', '*****@*****.**')); $this->assertStringMatchesFormat("%AEmail Address: admin@sitegear.org%A", HtmlUtilities::exception($e, 'Your Administrator', '*****@*****.**')); }
/** * See the documentation for __call() for details. * * @return string Rendered content. */ public function __toString() { try { // Delegate to the render() method. return $this->render(); } catch (\Exception $exception) { // Throwing any Exception from __toString() is a fatal error (bravo, php, bra-vo) so we have no other // choice but to catch any exception here and do something with it. Delete any buffered content and // display a plain error page. // TODO Make this a proper error-500 page but prevent feedback loops while (ob_get_level() > 1) { ob_end_clean(); } return HtmlUtilities::exception($exception, $this->getEngine()->getSiteInfo()->getAdministratorName(), $this->getEngine()->getSiteInfo()->getAdministratorEmail()); } }