Create the ErrorHandling middleware

Signed-off-by: Maurício Meneghini Fauth <mauricio@fauth.dev>
This commit is contained in:
Maurício Meneghini Fauth 2023-07-29 20:31:52 -03:00
parent daad3ab13a
commit 5dc23aec19
No known key found for this signature in database
GPG Key ID: 6A16FD38AFC89CC8
3 changed files with 49 additions and 14 deletions

View File

@ -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(

View File

@ -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;
}
/**

View 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;
}
}