Skip to content

Commit 0b49de3

Browse files
authored
Merge pull request #6286 from kenjis/refactor-exception-exit-code
refactor: Exception exit code
2 parents 3800c5d + 7900de2 commit 0b49de3

File tree

7 files changed

+24
-10
lines changed

7 files changed

+24
-10
lines changed

system/Database/Exceptions/DatabaseException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ class DatabaseException extends Error implements ExceptionInterface
2020
*
2121
* @var int
2222
*/
23-
protected $code = 8;
23+
protected $code = EXIT_DATABASE;
2424
}

system/Entity/Exceptions/CastException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
/**
1717
* 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.
1822
*/
1923
class CastException extends FrameworkException
2024
{

system/Exceptions/CastException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ class CastException extends CriticalError
2323
use DebugTraceableTrait;
2424

2525
/**
26-
* Error code
26+
* Exit status code
2727
*
2828
* @var int
2929
*/
30-
protected $code = 3;
30+
protected $code = EXIT_CONFIG;
3131

3232
public static function forInvalidJsonFormatException(int $error)
3333
{

system/Exceptions/ConfigException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ class ConfigException extends CriticalError
1919
use DebugTraceableTrait;
2020

2121
/**
22-
* Error code
22+
* Exit status code
2323
*
2424
* @var int
2525
*/
26-
protected $code = 3;
26+
protected $code = EXIT_CONFIG;
2727

2828
public static function forDisabledMigrations()
2929
{

system/Exceptions/PageNotFoundException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class PageNotFoundException extends OutOfBoundsException implements ExceptionInt
1919
use DebugTraceableTrait;
2020

2121
/**
22-
* Error code
22+
* HTTP status code
2323
*
2424
* @var int
2525
*/

system/Router/Exceptions/RedirectException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
class RedirectException extends Exception
2020
{
2121
/**
22-
* Status code for redirects
22+
* HTTP status code for redirects
2323
*
2424
* @var int
2525
*/

tests/system/Debug/ExceptionsTest.php

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

1212
namespace CodeIgniter\Debug;
1313

14+
use CodeIgniter\Database\Exceptions\DatabaseException;
15+
use CodeIgniter\Entity\Exceptions\CastException;
16+
use CodeIgniter\Exceptions\ConfigException;
1417
use CodeIgniter\Exceptions\PageNotFoundException;
1518
use CodeIgniter\Test\CIUnitTestCase;
1619
use CodeIgniter\Test\ReflectionHelper;
@@ -57,9 +60,16 @@ public function testDetermineCodes(): void
5760
{
5861
$determineCodes = $this->getPrivateMethodInvoker($this->exception, 'determineCodes');
5962

60-
$this->assertSame([500, 9], $determineCodes(new RuntimeException('This.')));
61-
$this->assertSame([500, 1], $determineCodes(new RuntimeException('That.', 600)));
62-
$this->assertSame([404, 1], $determineCodes(new RuntimeException('There.', 404)));
63+
$this->assertSame([500, EXIT__AUTO_MIN], $determineCodes(new RuntimeException('This.')));
64+
$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.')));
6373
}
6474

6575
public function testRenderBacktrace(): void

0 commit comments

Comments
 (0)