Skip to content

Commit 0581338

Browse files
committed
fix: incorrect HTTP status code may return
1 parent 730fc60 commit 0581338

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

system/Debug/Exceptions.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace CodeIgniter\Debug;
1313

1414
use CodeIgniter\API\ResponseTrait;
15+
use CodeIgniter\Exceptions\HasExitCodeException;
16+
use CodeIgniter\Exceptions\HasHttpStatusCodeException;
1517
use CodeIgniter\Exceptions\PageNotFoundException;
1618
use CodeIgniter\HTTP\CLIRequest;
1719
use CodeIgniter\HTTP\IncomingRequest;
@@ -304,18 +306,25 @@ protected function maskSensitiveData(&$trace, array $keysToMask, string $path =
304306
*/
305307
protected function determineCodes(Throwable $exception): array
306308
{
307-
$statusCode = abs($exception->getCode());
309+
$statusCode = 500;
310+
$exitStatus = EXIT_ERROR;
308311

309-
if ($statusCode < 100 || $statusCode > 599) {
310-
$exitStatus = $statusCode + EXIT__AUTO_MIN;
312+
// For backward compatibility. I don't think this process is necessary.
313+
$exceptionCode = abs($exception->getCode());
314+
if ($exceptionCode < 100 || $exceptionCode > 599) {
315+
$exitStatus = $exceptionCode + EXIT__AUTO_MIN;
311316

312317
if ($exitStatus > EXIT__AUTO_MAX) {
313318
$exitStatus = EXIT_ERROR;
314319
}
320+
}
321+
322+
if ($exception instanceof HasHttpStatusCodeException) {
323+
$statusCode = $exception->getCode();
324+
}
315325

316-
$statusCode = 500;
317-
} else {
318-
$exitStatus = EXIT_ERROR;
326+
if ($exception instanceof HasExitCodeException) {
327+
$exitStatus = $exception->getCode();
319328
}
320329

321330
return [$statusCode, $exitStatus];

tests/system/Debug/ExceptionsTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@ public function testDetermineCodes(): void
5858
$determineCodes = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');
5959

6060
$this->assertSame([500, 9], $determineCodes(new RuntimeException('This.')));
61+
$this->assertSame([500, 1], $determineCodes(new RuntimeException('This.', 167)));
6162
$this->assertSame([500, 1], $determineCodes(new RuntimeException('That.', 600)));
62-
$this->assertSame([404, 1], $determineCodes(new RuntimeException('There.', 404)));
63+
$this->assertSame([500, 1], $determineCodes(new RuntimeException('There.', 404)));
6364
}
6465

6566
public function testRenderBacktrace(): void

0 commit comments

Comments
 (0)