Skip to content

Commit 7232b09

Browse files
committed
feat: improve error messages
1 parent 23a4fb8 commit 7232b09

File tree

11 files changed

+43
-17
lines changed

11 files changed

+43
-17
lines changed

system/Database/DataConverter/Cast/ArrayCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ArrayCast extends BaseCast implements CastInterface
2323
public static function fromDatabase(mixed $value, array $params = []): array
2424
{
2525
if (! is_string($value)) {
26-
self::invalidTypeValueError($value);
26+
self::invalidTypeValueError($value, __CLASS__);
2727
}
2828

2929
if ((strpos($value, 'a:') === 0 || strpos($value, 's:') === 0)) {

system/Database/DataConverter/Cast/BaseCast.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,13 @@ public static function toDatabase(mixed $value, array $params = []): mixed
3535
/**
3636
* @throws TypeError
3737
*/
38-
protected static function invalidTypeValueError(mixed $value): never
38+
protected static function invalidTypeValueError(mixed $value, string $class): never
3939
{
40-
throw new TypeError('Invalid type: ' . get_debug_type($value));
40+
$message = '[' . $class . '] Invalid value type: ' . get_debug_type($value);
41+
if (is_scalar($value)) {
42+
$message .= ', and its value: ' . $value;
43+
}
44+
45+
throw new TypeError($message);
4146
}
4247
}

system/Database/DataConverter/Cast/CSVCast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CSVCast extends BaseCast
2626
public static function fromDatabase(mixed $value, array $params = []): array
2727
{
2828
if (! is_string($value)) {
29-
self::invalidTypeValueError($value);
29+
self::invalidTypeValueError($value, __CLASS__);
3030
}
3131

3232
return explode(',', $value);
@@ -38,7 +38,7 @@ public static function fromDatabase(mixed $value, array $params = []): array
3838
public static function toDatabase(mixed $value, array $params = []): string
3939
{
4040
if (! is_array($value)) {
41-
self::invalidTypeValueError($value);
41+
self::invalidTypeValueError($value, __CLASS__);
4242
}
4343

4444
return implode(',', $value);

system/Database/DataConverter/Cast/DatetimeCast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class DatetimeCast extends BaseCast
3131
public static function fromDatabase(mixed $value, array $params = []): Time
3232
{
3333
if (! is_string($value)) {
34-
self::invalidTypeValueError($value);
34+
self::invalidTypeValueError($value, __CLASS__);
3535
}
3636

3737
return Time::parse($value);
@@ -43,7 +43,7 @@ public static function fromDatabase(mixed $value, array $params = []): Time
4343
public static function toDatabase(mixed $value, array $params = []): string
4444
{
4545
if (! $value instanceof Time) {
46-
self::invalidTypeValueError($value);
46+
self::invalidTypeValueError($value, __CLASS__);
4747
}
4848

4949
return (string) $value;

system/Database/DataConverter/Cast/IntBoolCast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ final class IntBoolCast extends BaseCast
2626
public static function fromDatabase(mixed $value, array $params = []): bool
2727
{
2828
if (! is_int($value) && ! is_string($value)) {
29-
self::invalidTypeValueError($value);
29+
self::invalidTypeValueError($value, __CLASS__);
3030
}
3131

3232
return (bool) $value;
@@ -38,7 +38,7 @@ public static function fromDatabase(mixed $value, array $params = []): bool
3838
public static function toDatabase(mixed $value, array $params = []): int
3939
{
4040
if (! is_bool($value)) {
41-
self::invalidTypeValueError($value);
41+
self::invalidTypeValueError($value, __CLASS__);
4242
}
4343

4444
return (int) $value;

system/Database/DataConverter/Cast/IntegerCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class IntegerCast extends BaseCast
2626
public static function fromDatabase(mixed $value, array $params = []): int
2727
{
2828
if (! is_string($value) && ! is_int($value)) {
29-
self::invalidTypeValueError($value);
29+
self::invalidTypeValueError($value, __CLASS__);
3030
}
3131

3232
return (int) $value;

system/Database/DataConverter/Cast/JsonCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class JsonCast extends BaseCast
3030
public static function fromDatabase(mixed $value, array $params = []): array|stdClass
3131
{
3232
if (! is_string($value)) {
33-
self::invalidTypeValueError($value);
33+
self::invalidTypeValueError($value, __CLASS__);
3434
}
3535

3636
$associative = in_array('array', $params, true);

system/Database/DataConverter/Cast/TimestampCast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class TimestampCast extends BaseCast
2828
public static function fromDatabase(mixed $value, array $params = []): Time
2929
{
3030
if (! is_int($value) && ! is_string($value)) {
31-
self::invalidTypeValueError($value);
31+
self::invalidTypeValueError($value, __CLASS__);
3232
}
3333

3434
return Time::createFromTimestamp((int) $value);
@@ -40,7 +40,7 @@ public static function fromDatabase(mixed $value, array $params = []): Time
4040
public static function toDatabase(mixed $value, array $params = []): int
4141
{
4242
if (! $value instanceof Time) {
43-
self::invalidTypeValueError($value);
43+
self::invalidTypeValueError($value, __CLASS__);
4444
}
4545

4646
return $value->getTimestamp();

system/Database/DataConverter/Cast/URICast.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class URICast extends BaseCast
2828
public static function fromDatabase(mixed $value, array $params = []): URI
2929
{
3030
if (! is_string($value)) {
31-
self::invalidTypeValueError($value);
31+
self::invalidTypeValueError($value, __CLASS__);
3232
}
3333

3434
return new URI($value);
@@ -40,7 +40,7 @@ public static function fromDatabase(mixed $value, array $params = []): URI
4040
public static function toDatabase(mixed $value, array $params = []): string
4141
{
4242
if (! $value instanceof URI) {
43-
self::invalidTypeValueError($value);
43+
self::invalidTypeValueError($value, __CLASS__);
4444
}
4545

4646
return (string) $value;

system/Database/DataConverter/DataConverter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ protected function castAs($value, string $column, string $method = 'fromDatabase
157157
$handlers = array_merge($this->defaultCastHandlers, $this->castHandlers);
158158

159159
if (! isset($handlers[$type])) {
160-
throw new InvalidArgumentException('No such handler. Invalid type: ' . $type);
160+
throw new InvalidArgumentException('No such handler for "' . $column . '". Invalid type: ' . $type);
161161
}
162162

163163
$handler = $handlers[$type];

tests/system/Database/DataConverter/DataConverterTest.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use CodeIgniter\I18n\Time;
1616
use CodeIgniter\Test\CIUnitTestCase;
1717
use InvalidArgumentException;
18+
use TypeError;
1819

1920
/**
2021
* @internal
@@ -431,7 +432,7 @@ public function testURIConvertDataToDB(): void
431432
public function testInvalidType(): void
432433
{
433434
$this->expectException(InvalidArgumentException::class);
434-
$this->expectExceptionMessage('No such handler. Invalid type: invalid');
435+
$this->expectExceptionMessage('No such handler for "id". Invalid type: invalid');
435436

436437
$types = [
437438
'id' => 'invalid',
@@ -446,6 +447,26 @@ public function testInvalidType(): void
446447
$converter->fromDatabase($dbData);
447448
}
448449

450+
public function testInvalidValue(): void
451+
{
452+
$this->expectException(TypeError::class);
453+
$this->expectExceptionMessage(
454+
'[CodeIgniter\Database\DataConverter\Cast\JsonCast] Invalid value type: bool, and its value: 1'
455+
);
456+
457+
$types = [
458+
'id' => 'int',
459+
'remark' => 'json-array',
460+
];
461+
$converter = $this->createDataConverter($types);
462+
463+
$dbData = [
464+
'id' => '1',
465+
'remark' => true,
466+
];
467+
$converter->fromDatabase($dbData);
468+
}
469+
449470
public function testInvalidCastHandler(): void
450471
{
451472
$this->expectException(InvalidArgumentException::class);

0 commit comments

Comments
 (0)