mirror of
https://github.com/flarum/framework.git
synced 2024-12-13 07:03:35 +08:00
Implement middleware for presenting pretty error pages
This commit is contained in:
parent
5dcc13ec11
commit
20b814642f
44
framework/core/src/Forum/Middleware/HandleErrors.php
Normal file
44
framework/core/src/Forum/Middleware/HandleErrors.php
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
<?php namespace Flarum\Forum\Middleware;
|
||||||
|
|
||||||
|
use Psr\Http\Message\ResponseInterface as Response;
|
||||||
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
|
use Zend\Diactoros\Response\StringResponse;
|
||||||
|
use Zend\Stratigility\ErrorMiddlewareInterface;
|
||||||
|
|
||||||
|
class HandleErrors implements ErrorMiddlewareInterface
|
||||||
|
{
|
||||||
|
protected $templateDir;
|
||||||
|
|
||||||
|
public function __construct($templateDir)
|
||||||
|
{
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user