diff --git a/system/Debug/ExceptionHandler.php b/system/Debug/ExceptionHandler.php index 63449888ffb5..09c6de0d5b7e 100644 --- a/system/Debug/ExceptionHandler.php +++ b/system/Debug/ExceptionHandler.php @@ -97,8 +97,8 @@ public function handle( . DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR . $addPath; // Determine the views - $view = $this->determineView($exception, $path); - $altView = $this->determineView($exception, $altPath); + $view = $this->determineView($exception, $path, $statusCode); + $altView = $this->determineView($exception, $altPath, $statusCode); // Check if the view exists $viewFile = null; @@ -119,13 +119,16 @@ public function handle( } /** - * Determines the view to display based on the exception thrown, - * whether an HTTP or CLI request, etc. + * Determines the view to display based on the exception thrown, HTTP status + * code, whether an HTTP or CLI request, etc. * * @return string The filename of the view file to use */ - protected function determineView(Throwable $exception, string $templatePath): string - { + protected function determineView( + Throwable $exception, + string $templatePath, + int $statusCode = 500 + ): string { // Production environments should have a custom exception file. $view = 'production.php'; @@ -147,8 +150,8 @@ protected function determineView(Throwable $exception, string $templatePath): st $templatePath = rtrim($templatePath, '\\/ ') . DIRECTORY_SEPARATOR; // Allow for custom views based upon the status code - if (is_file($templatePath . 'error_' . $exception->getCode() . '.php')) { - return 'error_' . $exception->getCode() . '.php'; + if (is_file($templatePath . 'error_' . $statusCode . '.php')) { + return 'error_' . $statusCode . '.php'; } return $view; diff --git a/tests/system/Debug/ExceptionHandlerTest.php b/tests/system/Debug/ExceptionHandlerTest.php index 8dcad8e6c338..a55227ab7a34 100644 --- a/tests/system/Debug/ExceptionHandlerTest.php +++ b/tests/system/Debug/ExceptionHandlerTest.php @@ -69,7 +69,7 @@ public function testDetermineViewsRuntimeExceptionCode404(): void $templatePath = APPPATH . 'Views/errors/html'; $viewFile = $determineView($exception, $templatePath); - $this->assertSame('error_404.php', $viewFile); + $this->assertSame('error_exception.php', $viewFile); } public function testDetermineViewsDisplayErrorsOffRuntimeException(): void diff --git a/user_guide_src/source/changelogs/v4.4.8.rst b/user_guide_src/source/changelogs/v4.4.8.rst index 5e3eb3d16908..4ff846be528a 100644 --- a/user_guide_src/source/changelogs/v4.4.8.rst +++ b/user_guide_src/source/changelogs/v4.4.8.rst @@ -14,6 +14,11 @@ Release Date: Unreleased BREAKING ******** +- A bug that caused the :doc:`Exception handler <../general/errors>` to display + incorrect error view file corresponding to the exception code has been fixed. + The third parameter ``int $statusCode = 500`` has been added to + ``CodeIgniter\Debug\ExceptionHandler::determineView()`` for this purpose. + *************** Message Changes ***************