From d2d65cdcdb2e193b107b0c286ed040e82fc5434b Mon Sep 17 00:00:00 2001 From: kenjis Date: Wed, 8 Feb 2023 11:30:30 +0900 Subject: [PATCH] feat: use ExceptionHandler --- system/Debug/Exceptions.php | 46 ++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/system/Debug/Exceptions.php b/system/Debug/Exceptions.php index 6dc098d4af10..163a96f94962 100644 --- a/system/Debug/Exceptions.php +++ b/system/Debug/Exceptions.php @@ -15,12 +15,12 @@ use CodeIgniter\Exceptions\HasExitCodeInterface; use CodeIgniter\Exceptions\HTTPExceptionInterface; use CodeIgniter\Exceptions\PageNotFoundException; -use CodeIgniter\HTTP\CLIRequest; use CodeIgniter\HTTP\Exceptions\HTTPException; -use CodeIgniter\HTTP\IncomingRequest; +use CodeIgniter\HTTP\RequestInterface; use CodeIgniter\HTTP\ResponseInterface; use Config\Exceptions as ExceptionsConfig; use Config\Paths; +use Config\Services; use ErrorException; use Psr\Log\LogLevel; use Throwable; @@ -36,6 +36,8 @@ class Exceptions * Nesting level of the output buffering mechanism * * @var int + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ public $ob_level; @@ -44,6 +46,8 @@ class Exceptions * cli and html error view directories. * * @var string + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ protected $viewPath; @@ -57,7 +61,7 @@ class Exceptions /** * The request. * - * @var CLIRequest|IncomingRequest + * @var RequestInterface */ protected $request; @@ -71,12 +75,14 @@ class Exceptions private ?Throwable $exceptionCaughtByExceptionHandler = null; /** - * @param CLIRequest|IncomingRequest $request + * @param RequestInterface $request */ public function __construct(ExceptionsConfig $config, $request, ResponseInterface $response) { + // For backward compatibility $this->ob_level = ob_get_level(); $this->viewPath = rtrim($config->errorViewPath, '\\/ ') . DIRECTORY_SEPARATOR; + $this->config = $config; $this->request = $request; $this->response = $response; @@ -110,8 +116,6 @@ public function initialize() * Catches any uncaught errors and exceptions, including most Fatal errors * (Yay PHP7!). Will log the error, display it if display_errors is on, * and fire an event that allows custom actions to be taken at this point. - * - * @codeCoverageIgnore */ public function exceptionHandler(Throwable $exception) { @@ -119,6 +123,24 @@ public function exceptionHandler(Throwable $exception) [$statusCode, $exitCode] = $this->determineCodes($exception); + $this->request = Services::request(); + $this->response = Services::response(); + + if (method_exists($this->config, 'handler')) { + // Use new ExceptionHandler + $handler = $this->config->handler($statusCode, $exception); + $handler->handle( + $exception, + $this->request, + $this->response, + $statusCode, + $exitCode + ); + + return; + } + + // For backward compatibility if ($this->config->log === true && ! in_array($statusCode, $this->config->ignoreCodes, true)) { log_message('critical', "{message}\nin {exFile} on line {exLine}.\n{trace}", [ 'message' => $exception->getMessage(), @@ -212,6 +234,8 @@ public function shutdownHandler() * whether an HTTP or CLI request, etc. * * @return string The path and filename of the view file to use + * + * @deprecated No longer used. Moved to ExceptionHandler. */ protected function determineView(Throwable $exception, string $templatePath): string { @@ -238,6 +262,8 @@ protected function determineView(Throwable $exception, string $templatePath): st /** * Given an exception and status code will display the error to the client. + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ protected function render(Throwable $exception, int $statusCode) { @@ -282,6 +308,8 @@ protected function render(Throwable $exception, int $statusCode) /** * Gathers the variables that will be made available to the view. + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ protected function collectVars(Throwable $exception, int $statusCode): array { @@ -306,6 +334,8 @@ protected function collectVars(Throwable $exception, int $statusCode): array * Mask sensitive data in the trace. * * @param array|object $trace + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ protected function maskSensitiveData(&$trace, array $keysToMask, string $path = '') { @@ -416,6 +446,8 @@ public static function cleanPath(string $file): string /** * Describes memory usage in real-world units. Intended for use * with memory_get_usage, etc. + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ public static function describeMemory(int $bytes): string { @@ -434,6 +466,8 @@ public static function describeMemory(int $bytes): string * Creates a syntax-highlighted version of a PHP file. * * @return bool|string + * + * @deprecated No longer used. Moved to BaseExceptionHandler. */ public static function highlightFile(string $file, int $lineNumber, int $lines = 15) {