diff --git a/libraries/classes/Application.php b/libraries/classes/Application.php index f23f03e693..16ba9672e2 100644 --- a/libraries/classes/Application.php +++ b/libraries/classes/Application.php @@ -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( diff --git a/libraries/classes/ErrorHandler.php b/libraries/classes/ErrorHandler.php index 7d628fd2d8..b48d431f9a 100644 --- a/libraries/classes/ErrorHandler.php +++ b/libraries/classes/ErrorHandler.php @@ -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(''); - $response->callExit(); + + if (defined('TESTSUITE')) { + throw new ExitException(); + } + + (new SapiEmitter())->emit($response->response()->withStatus(StatusCodeInterface::STATUS_INTERNAL_SERVER_ERROR)); + + exit; } /** diff --git a/libraries/classes/Middleware/ErrorHandling.php b/libraries/classes/Middleware/ErrorHandling.php new file mode 100644 index 0000000000..a68edc6839 --- /dev/null +++ b/libraries/classes/Middleware/ErrorHandling.php @@ -0,0 +1,36 @@ +errorHandler->handleException(...)); + set_error_handler($this->errorHandler->handleError(...)); + + $response = $handler->handle($request); + + restore_error_handler(); + restore_exception_handler(); + + return $response; + } +}