Skip to content

Commit

Permalink
Merge pull request #8689 from kenjis/fix-ExceptionHandler-determineView
Browse files Browse the repository at this point in the history
fix: the error view is determined by Exception code
  • Loading branch information
kenjis authored Apr 2, 2024
2 parents d5660ce + b651344 commit 22db69c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
19 changes: 11 additions & 8 deletions system/Debug/ExceptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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';

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion tests/system/Debug/ExceptionHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/changelogs/v4.4.8.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
***************
Expand Down

0 comments on commit 22db69c

Please sign in to comment.