From cfd1b44c0cef1ee3c047987ca4fc771aae64452e Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 23 Jul 2022 07:42:09 +0900 Subject: [PATCH 1/5] refactor: replace magic numbers with constants --- system/Database/Exceptions/DatabaseException.php | 2 +- system/Exceptions/CastException.php | 4 ++-- system/Exceptions/ConfigException.php | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/system/Database/Exceptions/DatabaseException.php b/system/Database/Exceptions/DatabaseException.php index a2b3b7637ce7..13f58dc12954 100644 --- a/system/Database/Exceptions/DatabaseException.php +++ b/system/Database/Exceptions/DatabaseException.php @@ -20,5 +20,5 @@ class DatabaseException extends Error implements ExceptionInterface * * @var int */ - protected $code = 8; + protected $code = EXIT_DATABASE; } diff --git a/system/Exceptions/CastException.php b/system/Exceptions/CastException.php index 5e41631536c7..06511f73907f 100644 --- a/system/Exceptions/CastException.php +++ b/system/Exceptions/CastException.php @@ -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) { diff --git a/system/Exceptions/ConfigException.php b/system/Exceptions/ConfigException.php index 41ae4959e594..ced8afc6e04f 100644 --- a/system/Exceptions/ConfigException.php +++ b/system/Exceptions/ConfigException.php @@ -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() { From 876194eeb7ea03820431854530acd37ad3c279d3 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 23 Jul 2022 07:44:20 +0900 Subject: [PATCH 2/5] docs: update comments --- system/Exceptions/PageNotFoundException.php | 2 +- system/Router/Exceptions/RedirectException.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/system/Exceptions/PageNotFoundException.php b/system/Exceptions/PageNotFoundException.php index 2a773f1785dc..531df0e48f11 100644 --- a/system/Exceptions/PageNotFoundException.php +++ b/system/Exceptions/PageNotFoundException.php @@ -19,7 +19,7 @@ class PageNotFoundException extends OutOfBoundsException implements ExceptionInt use DebugTraceableTrait; /** - * Error code + * HTTP status code * * @var int */ diff --git a/system/Router/Exceptions/RedirectException.php b/system/Router/Exceptions/RedirectException.php index b2456f367676..d5a1616b481b 100644 --- a/system/Router/Exceptions/RedirectException.php +++ b/system/Router/Exceptions/RedirectException.php @@ -19,7 +19,7 @@ class RedirectException extends Exception { /** - * Status code for redirects + * HTTP status code for redirects * * @var int */ From 56e3af04ee0e9b67da86f6b78e8651d81ae73f14 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 23 Jul 2022 07:52:00 +0900 Subject: [PATCH 3/5] test: replace magic numbers with constants --- tests/system/Debug/ExceptionsTest.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index 8bd4f01e0925..2c1227cba008 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -57,9 +57,9 @@ 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))); } public function testRenderBacktrace(): void From e52c06f30dace76e54f3dd961b84afda445d01b7 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 23 Jul 2022 07:52:19 +0900 Subject: [PATCH 4/5] test: add assertions --- tests/system/Debug/ExceptionsTest.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/system/Debug/ExceptionsTest.php b/tests/system/Debug/ExceptionsTest.php index 2c1227cba008..3ef643c831e3 100644 --- a/tests/system/Debug/ExceptionsTest.php +++ b/tests/system/Debug/ExceptionsTest.php @@ -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; @@ -60,6 +63,13 @@ public function testDetermineCodes(): void $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 From 7900de26bc66d7702cea195e4958c976032d1f94 Mon Sep 17 00:00:00 2001 From: kenjis Date: Sat, 23 Jul 2022 08:01:05 +0900 Subject: [PATCH 5/5] docs: add @TODO --- system/Entity/Exceptions/CastException.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/system/Entity/Exceptions/CastException.php b/system/Entity/Exceptions/CastException.php index 2cda4d72238d..224bbdc7eb68 100644 --- a/system/Entity/Exceptions/CastException.php +++ b/system/Entity/Exceptions/CastException.php @@ -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 {