New design for error pages. closes #252

This commit is contained in:
Toby Zerner 2017-11-29 13:03:55 +10:30
parent 68ca96cb7b
commit ca1f2d8a80
7 changed files with 38 additions and 84 deletions

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>403 Forbidden</h1>
<p>You do not have permissions to access this page.</p>
</body>
</html>

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>404 Not Found</h1>
<p>Looks like this page could not be found.</p>
</body>
</html>

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>500 Internal Server Error</h1>
<p>Something went wrong on our server.</p>
</body>
</html>

View File

@ -1,13 +0,0 @@
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>503 Service Unavailable</h1>
<p>This forum is down for maintenance.</p>
</body>
</html>

View File

@ -11,11 +11,10 @@
namespace Flarum\Forum;
use Exception;
use Flarum\Event\ConfigureMiddleware;
use Flarum\Foundation\Application;
use Flarum\Http\AbstractServer;
use Flarum\Http\Middleware\HandleErrors;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Stratigility\MiddlewarePipe;
class Server extends AbstractServer
@ -29,30 +28,27 @@ class Server extends AbstractServer
$pipe->raiseThrowables();
$path = parse_url($app->url(), PHP_URL_PATH);
$errorDir = __DIR__.'/../../error';
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\HandleErrors', ['debug' => $app->inDebugMode() || ! $app->isInstalled()]));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
if (! $app->isInstalled()) {
$app->register('Flarum\Install\InstallServiceProvider');
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), true));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.install.routes')]));
} elseif ($app->isUpToDate() && ! $app->isDownForMaintenance()) {
$pipe->pipe($path, new HandleErrors($errorDir, $app->make('log'), $app->inDebugMode()));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ParseJsonBody'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\StartSession'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\RememberFromCookie'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\AuthenticateWithSession'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\SetLocale'));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ShareErrorsFromSession'));
event(new ConfigureMiddleware($pipe, $path, $this));
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')]));
} else {
$pipe->pipe($path, function () use ($errorDir) {
return new HtmlResponse(file_get_contents($errorDir.'/503.html', 503));
$pipe->pipe($path, function () {
throw new Exception('', 503);
});
}

View File

@ -13,6 +13,7 @@ namespace Flarum\Http\Middleware;
use Exception;
use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
@ -21,9 +22,9 @@ use Zend\Diactoros\Response\HtmlResponse;
class HandleErrors
{
/**
* @var string
* @var ViewFactory
*/
protected $templateDir;
protected $view;
/**
* @var LoggerInterface
@ -36,13 +37,13 @@ class HandleErrors
protected $debug;
/**
* @param string $templateDir
* @param ViewFactory $view
* @param LoggerInterface $logger
* @param bool $debug
*/
public function __construct($templateDir, LoggerInterface $logger, $debug = false)
public function __construct(ViewFactory $view, LoggerInterface $logger, $debug = false)
{
$this->templateDir = $templateDir;
$this->view = $view;
$this->logger = $logger;
$this->debug = $debug;
}
@ -75,7 +76,7 @@ class HandleErrors
$status = $errorCode;
}
if ($this->debug && ! in_array($errorCode, [403, 404])) {
if ($this->debug) {
$whoops = new WhoopsMiddleware;
return $whoops($error, $request, $response, $out);
@ -84,21 +85,8 @@ class HandleErrors
// Log the exception (with trace)
$this->logger->debug($error);
$errorPage = $this->getErrorPage($status);
$view = $this->view->make('flarum::error')->with('error', $error);
return new HtmlResponse($errorPage, $status);
}
/**
* @param string $status
* @return string
*/
protected function getErrorPage($status)
{
if (! file_exists($errorPage = $this->templateDir."/$status.html")) {
$errorPage = $this->templateDir.'/500.html';
}
return file_get_contents($errorPage);
return new HtmlResponse($view->render(), $status);
}
}

View File

@ -0,0 +1,22 @@
@extends('flarum.forum::layouts.basic')
@section('content')
<p>
{{-- TODO: Change below to @php when Laravel is upgraded --}}
<?php
$getTranslationIfExists = function ($code) use ($translator, $settings) {
$key = 'core.views.error.'.$code.'_message';
$translation = $translator->trans($key, ['{forum}' => $settings->get('forum_title')]);
return $translation === $key ? false : $translation;
};
if (! $translation = $getTranslationIfExists($error->getCode())) {
if (! $translation = $getTranslationIfExists(500)) {
$translation = 'An error occurred while trying to load this page.';
}
}
?>
{{ $translation }}
</p>
@endsection