mirror of
https://github.com/flarum/core.git
synced 2025-08-03 15:07:53 +02:00
Split up HandleErrors middleware into distinct classes
These are completely distinct functionalities, toggled through the system-wide debug flag. By moving the selection of the middleware to use to the place where the middleware pipe is built, we make the middleware itself be unaware of these flags. The two classes are more focused on what they are doing, with the constructor dependencies clearly representing their requirements. In addition, this means we can just use the HandleErrorsWithWhoops middleware in the installer, which means we do not need to worry about how to inject a SettingsRepositoryInterface implementation when flarum is not yet set up.
This commit is contained in:
@@ -13,7 +13,6 @@ namespace Flarum\Http\Middleware;
|
||||
|
||||
use Exception;
|
||||
use Flarum\Settings\SettingsRepositoryInterface;
|
||||
use Franzl\Middleware\Whoops\WhoopsRunner;
|
||||
use Illuminate\Contracts\View\Factory as ViewFactory;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
@@ -23,7 +22,7 @@ use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\Translation\TranslatorInterface;
|
||||
use Zend\Diactoros\Response\HtmlResponse;
|
||||
|
||||
class HandleErrors implements Middleware
|
||||
class HandleErrorsWithView implements Middleware
|
||||
{
|
||||
/**
|
||||
* @var ViewFactory
|
||||
@@ -45,25 +44,18 @@ class HandleErrors implements Middleware
|
||||
*/
|
||||
protected $settings;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
protected $debug;
|
||||
|
||||
/**
|
||||
* @param ViewFactory $view
|
||||
* @param LoggerInterface $logger
|
||||
* @param TranslatorInterface $translator
|
||||
* @param SettingsRepositoryInterface $settings
|
||||
* @param bool $debug
|
||||
*/
|
||||
public function __construct(ViewFactory $view, LoggerInterface $logger, TranslatorInterface $translator, SettingsRepositoryInterface $settings, $debug = false)
|
||||
public function __construct(ViewFactory $view, LoggerInterface $logger, TranslatorInterface $translator, SettingsRepositoryInterface $settings)
|
||||
{
|
||||
$this->view = $view;
|
||||
$this->logger = $logger;
|
||||
$this->translator = $translator;
|
||||
$this->settings = $settings;
|
||||
$this->debug = $debug;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -74,11 +66,7 @@ class HandleErrors implements Middleware
|
||||
try {
|
||||
return $handler->handle($request);
|
||||
} catch (Exception $e) {
|
||||
if ($this->debug) {
|
||||
return WhoopsRunner::handle($e, $request);
|
||||
} else {
|
||||
return $this->formatException($e);
|
||||
}
|
||||
return $this->formatException($e);
|
||||
}
|
||||
}
|
||||
|
34
src/Http/Middleware/HandleErrorsWithWhoops.php
Normal file
34
src/Http/Middleware/HandleErrorsWithWhoops.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Flarum.
|
||||
*
|
||||
* (c) Toby Zerner <toby.zerner@gmail.com>
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace Flarum\Http\Middleware;
|
||||
|
||||
use Exception;
|
||||
use Franzl\Middleware\Whoops\WhoopsRunner;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
use Psr\Http\Server\MiddlewareInterface as Middleware;
|
||||
use Psr\Http\Server\RequestHandlerInterface as Handler;
|
||||
|
||||
class HandleErrorsWithWhoops implements Middleware
|
||||
{
|
||||
/**
|
||||
* Catch all errors that happen during further middleware execution.
|
||||
*/
|
||||
public function process(Request $request, Handler $handler): Response
|
||||
{
|
||||
try {
|
||||
return $handler->handle($request);
|
||||
} catch (Exception $e) {
|
||||
return WhoopsRunner::handle($e, $request);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user