mirror of
https://github.com/flarum/framework.git
synced 2025-02-22 20:29:55 +08:00
New design for error pages. closes #252
This commit is contained in:
parent
68ca96cb7b
commit
ca1f2d8a80
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -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>
|
|
@ -11,11 +11,10 @@
|
|||||||
|
|
||||||
namespace Flarum\Forum;
|
namespace Flarum\Forum;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Flarum\Event\ConfigureMiddleware;
|
use Flarum\Event\ConfigureMiddleware;
|
||||||
use Flarum\Foundation\Application;
|
use Flarum\Foundation\Application;
|
||||||
use Flarum\Http\AbstractServer;
|
use Flarum\Http\AbstractServer;
|
||||||
use Flarum\Http\Middleware\HandleErrors;
|
|
||||||
use Zend\Diactoros\Response\HtmlResponse;
|
|
||||||
use Zend\Stratigility\MiddlewarePipe;
|
use Zend\Stratigility\MiddlewarePipe;
|
||||||
|
|
||||||
class Server extends AbstractServer
|
class Server extends AbstractServer
|
||||||
@ -29,30 +28,27 @@ class Server extends AbstractServer
|
|||||||
$pipe->raiseThrowables();
|
$pipe->raiseThrowables();
|
||||||
|
|
||||||
$path = parse_url($app->url(), PHP_URL_PATH);
|
$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()) {
|
if (! $app->isInstalled()) {
|
||||||
$app->register('Flarum\Install\InstallServiceProvider');
|
$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')]));
|
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.install.routes')]));
|
||||||
} elseif ($app->isUpToDate() && ! $app->isDownForMaintenance()) {
|
} 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\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\RememberFromCookie'));
|
||||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\AuthenticateWithSession'));
|
$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\SetLocale'));
|
||||||
|
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\ShareErrorsFromSession'));
|
||||||
|
|
||||||
event(new ConfigureMiddleware($pipe, $path, $this));
|
event(new ConfigureMiddleware($pipe, $path, $this));
|
||||||
|
|
||||||
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')]));
|
$pipe->pipe($path, $app->make('Flarum\Http\Middleware\DispatchRoute', ['routes' => $app->make('flarum.forum.routes')]));
|
||||||
} else {
|
} else {
|
||||||
$pipe->pipe($path, function () use ($errorDir) {
|
$pipe->pipe($path, function () {
|
||||||
return new HtmlResponse(file_get_contents($errorDir.'/503.html', 503));
|
throw new Exception('', 503);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,6 +13,7 @@ namespace Flarum\Http\Middleware;
|
|||||||
|
|
||||||
use Exception;
|
use Exception;
|
||||||
use Franzl\Middleware\Whoops\ErrorMiddleware as WhoopsMiddleware;
|
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\ResponseInterface as Response;
|
||||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
@ -21,9 +22,9 @@ use Zend\Diactoros\Response\HtmlResponse;
|
|||||||
class HandleErrors
|
class HandleErrors
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var ViewFactory
|
||||||
*/
|
*/
|
||||||
protected $templateDir;
|
protected $view;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var LoggerInterface
|
* @var LoggerInterface
|
||||||
@ -36,13 +37,13 @@ class HandleErrors
|
|||||||
protected $debug;
|
protected $debug;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $templateDir
|
* @param ViewFactory $view
|
||||||
* @param LoggerInterface $logger
|
* @param LoggerInterface $logger
|
||||||
* @param bool $debug
|
* @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->logger = $logger;
|
||||||
$this->debug = $debug;
|
$this->debug = $debug;
|
||||||
}
|
}
|
||||||
@ -75,7 +76,7 @@ class HandleErrors
|
|||||||
$status = $errorCode;
|
$status = $errorCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($this->debug && ! in_array($errorCode, [403, 404])) {
|
if ($this->debug) {
|
||||||
$whoops = new WhoopsMiddleware;
|
$whoops = new WhoopsMiddleware;
|
||||||
|
|
||||||
return $whoops($error, $request, $response, $out);
|
return $whoops($error, $request, $response, $out);
|
||||||
@ -84,21 +85,8 @@ class HandleErrors
|
|||||||
// Log the exception (with trace)
|
// Log the exception (with trace)
|
||||||
$this->logger->debug($error);
|
$this->logger->debug($error);
|
||||||
|
|
||||||
$errorPage = $this->getErrorPage($status);
|
$view = $this->view->make('flarum::error')->with('error', $error);
|
||||||
|
|
||||||
return new HtmlResponse($errorPage, $status);
|
return new HtmlResponse($view->render(), $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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
22
framework/core/views/error.blade.php
Normal file
22
framework/core/views/error.blade.php
Normal 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
|
Loading…
x
Reference in New Issue
Block a user