Skip to content

Commit c7bee50

Browse files
committed
fix: incorrect HTTP status code or Exit code may return
1 parent 336aae4 commit c7bee50

File tree

7 files changed

+19
-19
lines changed

7 files changed

+19
-19
lines changed

system/Database/Exceptions/DatabaseException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111

1212
namespace CodeIgniter\Database\Exceptions;
1313

14-
use CodeIgniter\Exceptions\HasExitCodeException;
14+
use CodeIgniter\Exceptions\ExitExceptionInterface;
1515
use Error;
1616

17-
class DatabaseException extends Error implements ExceptionInterface, HasExitCodeException
17+
class DatabaseException extends Error implements ExceptionInterface, ExitExceptionInterface
1818
{
1919
/**
2020
* Exit status code

system/Debug/Exceptions.php

Lines changed: 9 additions & 10 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\ExitExceptionInterface;
16+
use CodeIgniter\Exceptions\HTTPExceptionInterface;
1517
use CodeIgniter\Exceptions\PageNotFoundException;
1618
use CodeIgniter\HTTP\CLIRequest;
1719
use CodeIgniter\HTTP\Exceptions\HTTPException;
@@ -312,18 +314,15 @@ 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;
319-
320-
if ($exitStatus > EXIT__AUTO_MAX) {
321-
$exitStatus = EXIT_ERROR;
322-
}
320+
if ($exception instanceof HTTPExceptionInterface) {
321+
$statusCode = $exception->getCode();
322+
}
323323

324-
$statusCode = 500;
325-
} else {
326-
$exitStatus = EXIT_ERROR;
324+
if ($exception instanceof ExitExceptionInterface) {
325+
$exitStatus = $exception->getCode();
327326
}
328327

329328
return [$statusCode, $exitStatus];

system/Exceptions/CastException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*
1919
* @codeCoverageIgnore
2020
*/
21-
class CastException extends CriticalError implements HasExitCodeException
21+
class CastException extends CriticalError implements ExitExceptionInterface
2222
{
2323
use DebugTraceableTrait;
2424

system/Exceptions/ConfigException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
/**
1515
* Exception for automatic logging.
1616
*/
17-
class ConfigException extends CriticalError implements HasExitCodeException
17+
class ConfigException extends CriticalError implements ExitExceptionInterface
1818
{
1919
use DebugTraceableTrait;
2020

system/Exceptions/PageNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Config\Services;
1515
use OutOfBoundsException;
1616

17-
class PageNotFoundException extends OutOfBoundsException implements ExceptionInterface, HasHttpStatusCodeException
17+
class PageNotFoundException extends OutOfBoundsException implements ExceptionInterface, HTTPExceptionInterface
1818
{
1919
use DebugTraceableTrait;
2020

system/Router/Exceptions/RedirectException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
namespace CodeIgniter\Router\Exceptions;
1313

14-
use CodeIgniter\Exceptions\HasHttpStatusCodeException;
14+
use CodeIgniter\Exceptions\HTTPExceptionInterface;
1515
use Exception;
1616

1717
/**
1818
* RedirectException
1919
*/
20-
class RedirectException extends Exception implements HasHttpStatusCodeException
20+
class RedirectException extends Exception implements HTTPExceptionInterface
2121
{
2222
/**
2323
* Status code for redirects

tests/system/Debug/ExceptionsTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ public function testDetermineCodes(): void
5757
{
5858
$determineCodes = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');
5959

60-
$this->assertSame([500, 9], $determineCodes(new RuntimeException('This.')));
60+
$this->assertSame([500, 1], $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)