Skip to content

Commit a2277d7

Browse files
committed
fix: incorrect HTTP status code may return
1 parent b7fc837 commit a2277d7

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\Exceptions\HTTPException;
@@ -312,18 +314,25 @@ protected function maskSensitiveData(&$trace, array $keysToMask, string $path =
312314
*/
313315
protected function determineCodes(Throwable $exception): array
314316
{
315-
$statusCode = abs($exception->getCode());
317+
$statusCode = 500;
318+
$exitStatus = EXIT_ERROR;
316319

317-
if ($statusCode < 100 || $statusCode > 599) {
318-
$exitStatus = $statusCode + EXIT__AUTO_MIN;
320+
// For backward compatibility. I don't think this process is necessary.
321+
$exceptionCode = abs($exception->getCode());
322+
if ($exceptionCode < 100 || $exceptionCode > 599) {
323+
$exitStatus = $exceptionCode + EXIT__AUTO_MIN;
319324

320325
if ($exitStatus > EXIT__AUTO_MAX) {
321326
$exitStatus = EXIT_ERROR;
322327
}
328+
}
329+
330+
if ($exception instanceof HasHttpStatusCodeException) {
331+
$statusCode = $exception->getCode();
332+
}
323333

324-
$statusCode = 500;
325-
} else {
326-
$exitStatus = EXIT_ERROR;
334+
if ($exception instanceof HasExitCodeException) {
335+
$exitStatus = $exception->getCode();
327336
}
328337

329338
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)