1
0
mirror of https://github.com/flarum/core.git synced 2025-08-15 12:54:47 +02:00

Add a formatter for rendering errors via the frontend

This commit is contained in:
Alexander Skvortsov
2020-07-28 18:08:49 -04:00
parent 053b3fd96b
commit f879182ef7

View File

@@ -0,0 +1,60 @@
<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Foundation\ErrorHandling;
use Flarum\Frontend\Controller;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\Container\Container;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Symfony\Component\Translation\TranslatorInterface;
/**
* A formatter for turning caught exceptions into "pretty" HTML error pages.
*
* For certain known error types, we display pages with dedicated information
* relevant to this class of error, e.g. a page with a search form for HTTP 404
* "Not Found" errors. We look for templates in the `views/error` directory.
*
* If no specific template exists, a generic "Something went wrong" page will be
* displayed, optionally enriched with a more specific error message if found in
* the translation files.
*/
class FrontendFormatter implements HttpFormatter
{
/**
* @var Container
*/
protected $container;
/**
* @var TranslatorInterface
*/
protected $translator;
/**
* @var SettingsRepositoryInterface
*/
protected $settings;
public function __construct(Container $container, TranslatorInterface $translator, SettingsRepositoryInterface $settings)
{
$this->container = $container;
$this->translator = $translator;
$this->settings = $settings;
}
public function format(HandledError $error, Request $request): Response
{
$frontend = $this->container->make("flarum.frontend.forum");
return (new Controller($frontend))->handle($request)->withStatus($error->getStatusCode());
}
}