Skip to content

Commit e6bdf90

Browse files
committed
fix: bug that the error view is determined by Exception code
It should be determined by HTTP status code. Otherwise, if by chance an exception with code 404 is thrown, a 404 Not Found page will be displayed.
1 parent 67fa536 commit e6bdf90

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

system/Debug/ExceptionHandler.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ public function handle(
9797
. DIRECTORY_SEPARATOR . 'errors' . DIRECTORY_SEPARATOR . $addPath;
9898

9999
// Determine the views
100-
$view = $this->determineView($exception, $path);
101-
$altView = $this->determineView($exception, $altPath);
100+
$view = $this->determineView($exception, $path, $statusCode);
101+
$altView = $this->determineView($exception, $altPath, $statusCode);
102102

103103
// Check if the view exists
104104
$viewFile = null;
@@ -119,13 +119,16 @@ public function handle(
119119
}
120120

121121
/**
122-
* Determines the view to display based on the exception thrown,
123-
* whether an HTTP or CLI request, etc.
122+
* Determines the view to display based on the exception thrown, HTTP status
123+
* code, whether an HTTP or CLI request, etc.
124124
*
125125
* @return string The filename of the view file to use
126126
*/
127-
protected function determineView(Throwable $exception, string $templatePath): string
128-
{
127+
protected function determineView(
128+
Throwable $exception,
129+
string $templatePath,
130+
int $statusCode = 500
131+
): string {
129132
// Production environments should have a custom exception file.
130133
$view = 'production.php';
131134

@@ -147,8 +150,8 @@ protected function determineView(Throwable $exception, string $templatePath): st
147150
$templatePath = rtrim($templatePath, '\\/ ') . DIRECTORY_SEPARATOR;
148151

149152
// Allow for custom views based upon the status code
150-
if (is_file($templatePath . 'error_' . $exception->getCode() . '.php')) {
151-
return 'error_' . $exception->getCode() . '.php';
153+
if (is_file($templatePath . 'error_' . $statusCode . '.php')) {
154+
return 'error_' . $statusCode . '.php';
152155
}
153156

154157
return $view;

tests/system/Debug/ExceptionHandlerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public function testDetermineViewsRuntimeExceptionCode404(): void
6969
$templatePath = APPPATH . 'Views/errors/html';
7070
$viewFile = $determineView($exception, $templatePath);
7171

72-
$this->assertSame('error_404.php', $viewFile);
72+
$this->assertSame('error_exception.php', $viewFile);
7373
}
7474

7575
public function testDetermineViewsDisplayErrorsOffRuntimeException(): void

0 commit comments

Comments
 (0)