Skip to content

Commit dc51f1e

Browse files
committed
fix: incorrect HTTP status code or Exit code may return
1 parent 5e87f11 commit dc51f1e

File tree

8 files changed

+31
-31
lines changed

8 files changed

+31
-31
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/Entity/Exceptions/CastException.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@
1111

1212
namespace CodeIgniter\Entity\Exceptions;
1313

14+
use CodeIgniter\Exceptions\ExitExceptionInterface;
1415
use CodeIgniter\Exceptions\FrameworkException;
1516

1617
/**
1718
* CastException is thrown for invalid cast initialization and management.
18-
*
19-
* @TODO CodeIgniter\Exceptions\CastException is deprecated and this class is used.
20-
* CodeIgniter\Exceptions\CastException has the property $code = EXIT_CONFIG,
21-
* but this class does not have the code.
2219
*/
23-
class CastException extends FrameworkException
20+
class CastException extends FrameworkException implements ExitExceptionInterface
2421
{
22+
/**
23+
* Exit status code
24+
*
25+
* @var int
26+
*/
27+
protected $code = EXIT_CONFIG;
28+
2529
/**
2630
* Thrown when the cast class does not extends BaseCast.
2731
*

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
* HTTP status code for redirects

tests/system/Debug/ExceptionsTest.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,13 @@ public function testDetermineCodes(): void
6060
{
6161
$determineCodes = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');
6262

63-
$this->assertSame([500, EXIT__AUTO_MIN], $determineCodes(new RuntimeException('This.')));
63+
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('This.')));
6464
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('That.', 600)));
65-
$this->assertSame([404, EXIT_ERROR], $determineCodes(new RuntimeException('There.', 404)));
66-
$this->assertSame([167, EXIT_ERROR], $determineCodes(new RuntimeException('This.', 167)));
67-
// @TODO This exit code should be EXIT_CONFIG.
68-
$this->assertSame([500, 12], $determineCodes(new ConfigException('This.')));
69-
// @TODO This exit code should be EXIT_CONFIG.
70-
$this->assertSame([500, 9], $determineCodes(new CastException('This.')));
71-
// @TODO This exit code should be EXIT_DATABASE.
72-
$this->assertSame([500, 17], $determineCodes(new DatabaseException('This.')));
65+
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('There.', 404)));
66+
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('This.', 167)));
67+
$this->assertSame([500, EXIT_CONFIG], $determineCodes(new ConfigException('This.')));
68+
$this->assertSame([500, EXIT_CONFIG], $determineCodes(new CastException('This.')));
69+
$this->assertSame([500, EXIT_DATABASE], $determineCodes(new DatabaseException('This.')));
7370
}
7471

7572
public function testRenderBacktrace(): void

0 commit comments

Comments
 (0)