From b7c1cc5ceffb36bc990919bd749050f49f39554b Mon Sep 17 00:00:00 2001 From: Toby Zerner Date: Wed, 29 Nov 2017 13:03:55 +1030 Subject: [PATCH] New design for error pages. closes #252 --- error/403.html | 13 ------------ error/404.html | 13 ------------ error/500.html | 13 ------------ error/503.html | 13 ------------ src/Forum/Server.php | 18 +++++++---------- src/Http/Middleware/HandleErrors.php | 30 +++++++++------------------- views/error.blade.php | 22 ++++++++++++++++++++ 7 files changed, 38 insertions(+), 84 deletions(-) delete mode 100644 error/403.html delete mode 100644 error/404.html delete mode 100644 error/500.html delete mode 100644 error/503.html create mode 100644 views/error.blade.php diff --git a/error/403.html b/error/403.html deleted file mode 100644 index 1ed30a168..000000000 --- a/error/403.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - -

403 Forbidden

-

You do not have permissions to access this page.

- - - \ No newline at end of file diff --git a/error/404.html b/error/404.html deleted file mode 100644 index 262b80985..000000000 --- a/error/404.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - -

404 Not Found

-

Looks like this page could not be found.

- - - \ No newline at end of file diff --git a/error/500.html b/error/500.html deleted file mode 100644 index f9af9e116..000000000 --- a/error/500.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - -

500 Internal Server Error

-

Something went wrong on our server.

- - - diff --git a/error/503.html b/error/503.html deleted file mode 100644 index 46243a1c5..000000000 --- a/error/503.html +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - -

503 Service Unavailable

-

This forum is down for maintenance.

- - - diff --git a/src/Forum/Server.php b/src/Forum/Server.php index 893ddc52c..81072e964 100644 --- a/src/Forum/Server.php +++ b/src/Forum/Server.php @@ -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); }); } diff --git a/src/Http/Middleware/HandleErrors.php b/src/Http/Middleware/HandleErrors.php index 2790584a8..d7302332c 100644 --- a/src/Http/Middleware/HandleErrors.php +++ b/src/Http/Middleware/HandleErrors.php @@ -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); } } diff --git a/views/error.blade.php b/views/error.blade.php new file mode 100644 index 000000000..c7fca235c --- /dev/null +++ b/views/error.blade.php @@ -0,0 +1,22 @@ +@extends('flarum.forum::layouts.basic') + +@section('content') +

+ {{-- TODO: Change below to @php when Laravel is upgraded --}} + 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 }} +

+@endsection