Create the ErrorHandling middleware
Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
parent
daad3ab13a
commit
5dc23aec19
@ -22,6 +22,7 @@ use PhpMyAdmin\Http\Response;
|
||||
use PhpMyAdmin\Http\ServerRequest;
|
||||
use PhpMyAdmin\Identifiers\DatabaseName;
|
||||
use PhpMyAdmin\Identifiers\TableName;
|
||||
use PhpMyAdmin\Middleware\ErrorHandling;
|
||||
use PhpMyAdmin\Middleware\PhpExtensionsChecking;
|
||||
use PhpMyAdmin\Plugins\AuthenticationPlugin;
|
||||
use PhpMyAdmin\Plugins\AuthenticationPluginFactory;
|
||||
@ -80,6 +81,7 @@ class Application
|
||||
public function run(bool $isSetupPage = false): void
|
||||
{
|
||||
$requestHandler = new QueueRequestHandler(new ApplicationHandler($this));
|
||||
$requestHandler->add(new ErrorHandling($this->errorHandler));
|
||||
$requestHandler->add(new PhpExtensionsChecking($this, $this->template, $this->responseFactory));
|
||||
|
||||
$runner = new RequestHandlerRunner(
|
||||
|
||||
@ -5,6 +5,9 @@ declare(strict_types=1);
|
||||
namespace PhpMyAdmin;
|
||||
|
||||
use ErrorException;
|
||||
use Fig\Http\Message\StatusCodeInterface;
|
||||
use Laminas\HttpHandlerRunner\Emitter\SapiEmitter;
|
||||
use PhpMyAdmin\Exceptions\ExitException;
|
||||
use Throwable;
|
||||
|
||||
use function __;
|
||||
@ -13,8 +16,6 @@ use function count;
|
||||
use function defined;
|
||||
use function error_reporting;
|
||||
use function htmlspecialchars;
|
||||
use function set_error_handler;
|
||||
use function set_exception_handler;
|
||||
use function trigger_error;
|
||||
|
||||
use const E_COMPILE_ERROR;
|
||||
@ -57,17 +58,6 @@ class ErrorHandler
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
/**
|
||||
* Do not set ourselves as error handler in case of testsuite.
|
||||
*
|
||||
* This behavior is not tested there and breaks other tests as they
|
||||
* rely on PHPUnit doing it's own error handling which we break here.
|
||||
*/
|
||||
if (! defined('TESTSUITE')) {
|
||||
set_exception_handler($this->handleException(...));
|
||||
set_error_handler($this->handleError(...));
|
||||
}
|
||||
|
||||
if (! Util::isErrorReportingAvailable()) {
|
||||
return;
|
||||
}
|
||||
@ -334,7 +324,14 @@ class ErrorHandler
|
||||
|
||||
$response->addHTML($error->getDisplay());
|
||||
$response->addHTML('</body></html>');
|
||||
$response->callExit();
|
||||
|
||||
if (defined('TESTSUITE')) {
|
||||
throw new ExitException();
|
||||
}
|
||||
|
||||
(new SapiEmitter())->emit($response->response()->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR));
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
36
libraries/classes/Middleware/ErrorHandling.php
Normal file
36
libraries/classes/Middleware/ErrorHandling.php
Normal file
@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace PhpMyAdmin\Middleware;
|
||||
|
||||
use PhpMyAdmin\ErrorHandler;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Psr\Http\Server\MiddlewareInterface;
|
||||
use Psr\Http\Server\RequestHandlerInterface;
|
||||
|
||||
use function restore_error_handler;
|
||||
use function restore_exception_handler;
|
||||
use function set_error_handler;
|
||||
use function set_exception_handler;
|
||||
|
||||
final class ErrorHandling implements MiddlewareInterface
|
||||
{
|
||||
public function __construct(private readonly ErrorHandler $errorHandler)
|
||||
{
|
||||
}
|
||||
|
||||
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
|
||||
{
|
||||
set_exception_handler($this->errorHandler->handleException(...));
|
||||
set_error_handler($this->errorHandler->handleError(...));
|
||||
|
||||
$response = $handler->handle($request);
|
||||
|
||||
restore_error_handler();
|
||||
restore_exception_handler();
|
||||
|
||||
return $response;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user