1
0
mirror of https://github.com/flarum/core.git synced 2025-07-23 09:41:26 +02:00

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 dc29509321
commit c83e386ed2
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\Admin\Middleware\RequireAdministrateAbility'));
$pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\DispatchRoute', compact('routes'))); $pipe->pipe($adminPath, $app->make('Flarum\Http\Middleware\DispatchRoute', compact('routes')));
if ($app->inDebugMode()) { $pipe->pipe(new HandleErrors(__DIR__.'/../../error', $app->inDebugMode()));
$pipe->pipe(new WhoopsMiddleware);
} else {
$pipe->pipe(new HandleErrors(__DIR__.'/../../error'));
}
} }
return $pipe; return $pipe;

View File

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

View File

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