Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: the error view is determined by Exception code #8689

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading