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); + } +}