displayErrorDetails = (bool)$displayErrorDetails; } /** * Invoke error handler * * @param ServerRequestInterface $request The most recent Request object * @param ResponseInterface $response The most recent Response object * @param Exception $exception The caught Exception object * * @return ResponseInterface */ public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Exception $exception) { $contentType = $this->determineContentType($request); switch ($contentType) { case 'application/json': $output = $this->renderJsonErrorMessage($exception); break; case 'text/xml': case 'application/xml': $output = $this->renderXmlErrorMessage($exception); break; case 'text/html': $output = $this->renderHtmlErrorMessage($exception); break; } $body = new Body(fopen('php://temp', 'r+')); $body->write($output); return $response ->withStatus(500) ->withHeader('Content-type', $contentType) ->withBody($body); } /** * Render HTML error page * * @param Exception $exception * @return string */ protected function renderHtmlErrorMessage(Exception $exception) { $title = 'Slim Application Error'; if ($this->displayErrorDetails) { $html = '
The application could not run because of the following error:
'; $html .= 'A website error has occurred. Sorry for the temporary inconvenience.
'; } $output = sprintf( "" . "%s', htmlentities($trace)); } return $html; } /** * Render JSON error * * @param Exception $exception * @return string */ protected function renderJsonErrorMessage(Exception $exception) { $error = [ 'message' => 'Slim Application Error', ]; if ($this->displayErrorDetails) { $error['exception'] = []; do { $error['exception'][] = [ 'type' => get_class($exception), 'code' => $exception->getCode(), 'message' => $exception->getMessage(), 'file' => $exception->getFile(), 'line' => $exception->getLine(), 'trace' => explode("\n", $exception->getTraceAsString()), ]; } while ($exception = $exception->getPrevious()); } return json_encode($error, JSON_PRETTY_PRINT); } /** * Render XML error * * @param Exception $exception * @return string */ protected function renderXmlErrorMessage(Exception $exception) { $xml = "
" . $exception->getCode() . "\n";
$xml .= "