From 20b814642f26d91dccc9a01616a1cd703eadc8a7 Mon Sep 17 00:00:00 2001 From: Franz Liedke Date: Sat, 20 Jun 2015 22:39:33 +0200 Subject: [PATCH] Implement middleware for presenting pretty error pages --- .../src/Forum/Middleware/HandleErrors.php | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 framework/core/src/Forum/Middleware/HandleErrors.php diff --git a/framework/core/src/Forum/Middleware/HandleErrors.php b/framework/core/src/Forum/Middleware/HandleErrors.php new file mode 100644 index 000000000..1651685a1 --- /dev/null +++ b/framework/core/src/Forum/Middleware/HandleErrors.php @@ -0,0 +1,44 @@ +templateDir = $templateDir; + } + + /** + * {@inheritdoc} + */ + public function __invoke($error, Request $request, Response $response, callable $out = null) + { + $status = 500; + + // If it seems to be a valid HTTP status code, we pass on the + // exception's status. + $errorCode = $error->getCode(); + if (is_int($errorCode) && $errorCode >= 400 && $errorCode < 600) { + $status = $errorCode; + } + + $errorPage = $this->getErrorPage($status); + + return StringResponse::html($errorPage, $status); + } + + protected function getErrorPage($status) + { + if (!file_exists($errorPage = $this->templateDir."/$status.html")) { + $errorPage = $this->templateDir.'/500.html'; + } + + return file_get_contents($errorPage); + } +}