Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Exception exit code #6286

Merged
merged 5 commits into from
Jul 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion system/Database/Exceptions/DatabaseException.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ class DatabaseException extends Error implements ExceptionInterface
*
* @var int
*/
protected $code = 8;
protected $code = EXIT_DATABASE;
}
4 changes: 4 additions & 0 deletions system/Entity/Exceptions/CastException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@

/**
* CastException is thrown for invalid cast initialization and management.
*
* @TODO CodeIgniter\Exceptions\CastException is deprecated and this class is used.
* CodeIgniter\Exceptions\CastException has the property $code = EXIT_CONFIG,
* but this class does not have the code.
*/
class CastException extends FrameworkException
{
Expand Down
4 changes: 2 additions & 2 deletions system/Exceptions/CastException.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class CastException extends CriticalError
use DebugTraceableTrait;

/**
* Error code
* Exit status code
*
* @var int
*/
protected $code = 3;
protected $code = EXIT_CONFIG;

public static function forInvalidJsonFormatException(int $error)
{
Expand Down
4 changes: 2 additions & 2 deletions system/Exceptions/ConfigException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class ConfigException extends CriticalError
use DebugTraceableTrait;

/**
* Error code
* Exit status code
*
* @var int
*/
protected $code = 3;
protected $code = EXIT_CONFIG;

public static function forDisabledMigrations()
{
Expand Down
2 changes: 1 addition & 1 deletion system/Exceptions/PageNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class PageNotFoundException extends OutOfBoundsException implements ExceptionInt
use DebugTraceableTrait;

/**
* Error code
* HTTP status code
*
* @var int
*/
Expand Down
2 changes: 1 addition & 1 deletion system/Router/Exceptions/RedirectException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
class RedirectException extends Exception
{
/**
* Status code for redirects
* HTTP status code for redirects
*
* @var int
*/
Expand Down
16 changes: 13 additions & 3 deletions tests/system/Debug/ExceptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

namespace CodeIgniter\Debug;

use CodeIgniter\Database\Exceptions\DatabaseException;
use CodeIgniter\Entity\Exceptions\CastException;
use CodeIgniter\Exceptions\ConfigException;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\ReflectionHelper;
Expand Down Expand Up @@ -57,9 +60,16 @@ public function testDetermineCodes(): void
{
$determineCodes = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');

$this->assertSame([500, 9], $determineCodes(new RuntimeException('This.')));
$this->assertSame([500, 1], $determineCodes(new RuntimeException('That.', 600)));
$this->assertSame([404, 1], $determineCodes(new RuntimeException('There.', 404)));
$this->assertSame([500, EXIT__AUTO_MIN], $determineCodes(new RuntimeException('This.')));
$this->assertSame([500, EXIT_ERROR], $determineCodes(new RuntimeException('That.', 600)));
$this->assertSame([404, EXIT_ERROR], $determineCodes(new RuntimeException('There.', 404)));
$this->assertSame([167, EXIT_ERROR], $determineCodes(new RuntimeException('This.', 167)));
// @TODO This exit code should be EXIT_CONFIG.
$this->assertSame([500, 12], $determineCodes(new ConfigException('This.')));
// @TODO This exit code should be EXIT_CONFIG.
$this->assertSame([500, 9], $determineCodes(new CastException('This.')));
// @TODO This exit code should be EXIT_DATABASE.
$this->assertSame([500, 17], $determineCodes(new DatabaseException('This.')));
}

public function testRenderBacktrace(): void
Expand Down