Show 404 errors as the "pretty" page even in debug mode

closes #503
This commit is contained in:
Toby Zerner 2015-10-14 12:23:20 +10:30
parent 9772e398f6
commit 60bdbe6e52
3 changed files with 23 additions and 13 deletions

View File

@ -37,11 +37,8 @@ class Server extends AbstractServer
$pipe->pipe($adminPath, $app->make('Flarum\Admin\Middleware\RequireAdministrateAbility'));
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\DispatchRoute', compact('routes')));
if ($app->inDebugMode()) {
$pipe->pipe(new WhoopsMiddleware);
} else {
$pipe->pipe(new HandleErrors(__DIR__.'/../../error'));
}
$pipe->pipe(new HandleErrors(__DIR__.'/../../error', $app->inDebugMode()));
}
return $pipe;

View File

@ -15,7 +15,6 @@ use Flarum\Foundation\Application;
use Flarum\Http\AbstractServer;
use Zend\Stratigility\MiddlewarePipe;
use Flarum\Http\Middleware\HandleErrors;
use Franzl\Middleware\Whoops\Middleware as WhoopsMiddleware;
class Server extends AbstractServer
{
@ -44,11 +43,7 @@ class Server extends AbstractServer
$pipe->pipe($basePath, $app->make('Flarum\Http\Middleware\DispatchRoute', compact('routes')));
if ($app->inDebugMode() || ! $installed) {
$pipe->pipe(new WhoopsMiddleware);
} else {
$pipe->pipe(new HandleErrors(__DIR__.'/../../error'));
}
$pipe->pipe(new HandleErrors(__DIR__.'/../../error', $app->inDebugMode() || ! $installed));
return $pipe;
}

View File

@ -14,6 +14,7 @@ use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Stratigility\ErrorMiddlewareInterface;
use Franzl\Middleware\Whoops\Middleware as WhoopsMiddleware;
class HandleErrors implements ErrorMiddlewareInterface
{
@ -23,11 +24,18 @@ class HandleErrors implements ErrorMiddlewareInterface
protected $templateDir;
/**
* @param string $templateDir
* @var bool
*/
public function __construct($templateDir)
protected $debug;
/**
* @param string $templateDir
* @param bool $debug
*/
public function __construct($templateDir, $debug = false)
{
$this->templateDir = $templateDir;
$this->debug = $debug;
}
/**
@ -44,11 +52,21 @@ class HandleErrors implements ErrorMiddlewareInterface
$status = $errorCode;
}
if ($this->debug && $errorCode !== 404) {
$whoops = new WhoopsMiddleware;
return $whoops($error, $request, $response, $out);;
}
$errorPage = $this->getErrorPage($status);
return new HtmlResponse($errorPage, $status);
}
/**
* @param string $status
* @return string
*/
protected function getErrorPage($status)
{
if (! file_exists($errorPage = $this->templateDir."/$status.html")) {