Skip to content

Commit 4a10788

Browse files
committed
Adding context information when requesting invalid column type
1 parent 5fe0953 commit 4a10788

File tree

3 files changed

+68
-8
lines changed

3 files changed

+68
-8
lines changed

src/Schema/AbstractSchemaManager.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\Exception;
99
use Doctrine\DBAL\Exception\DatabaseRequired;
10+
use Doctrine\DBAL\Types\Exception\UnknownColumnType;
1011
use Doctrine\DBAL\Platforms\AbstractPlatform;
1112
use Doctrine\DBAL\Platforms\Exception\NotSupported;
1213
use Doctrine\DBAL\Result;
@@ -23,6 +24,7 @@
2324
use function count;
2425
use function func_get_arg;
2526
use function func_num_args;
27+
use function sprintf;
2628
use function strtolower;
2729

2830
/**
@@ -812,7 +814,11 @@ protected function _getPortableTableColumnList(string $table, string $database,
812814
{
813815
$list = [];
814816
foreach ($rows as $row) {
815-
$column = $this->_getPortableTableColumnDefinition($row);
817+
try {
818+
$column = $this->_getPortableTableColumnDefinition($row);
819+
} catch (UnknownColumnType $unknownTypeException) {
820+
throw UnknownColumnType::withContext($unknownTypeException->getType(), sprintf('table %s', $table));
821+
}
816822

817823
$name = strtolower($column->getQuotedName($this->platform));
818824
$list[$name] = $column;

src/Types/Exception/UnknownColumnType.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,45 @@
55
namespace Doctrine\DBAL\Types\Exception;
66

77
use Exception;
8+
use Throwable;
89

910
use function sprintf;
1011

1112
final class UnknownColumnType extends Exception implements TypesException
1213
{
13-
public static function new(string $name): self
14-
{
15-
return new self(
16-
sprintf(
17-
'Unknown column type "%s" requested. Any Doctrine type that you use has '
14+
private function __construct(
15+
private string $requestedType,
16+
?string $context,
17+
int $code = 0,
18+
?Throwable $throwable = null,
19+
) {
20+
$message = sprintf(
21+
'Unknown column type "%s" requested%s. Any Doctrine type that you use has '
1822
. 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the '
1923
. 'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database '
2024
. 'introspection then you might have forgotten to register all database types for a Doctrine Type. '
2125
. 'Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement '
2226
. 'Type#getMappedDatabaseTypes(). If the type name is empty you might '
2327
. 'have a problem with the cache or forgot some mapping information.',
24-
$name,
25-
),
28+
$this->requestedType,
29+
$context !== null ? ' for ' . $context : '',
2630
);
31+
32+
parent::__construct($message, $code, $throwable);
33+
}
34+
35+
public function getType(): string
36+
{
37+
return $this->requestedType;
38+
}
39+
40+
public static function new(string $name): self
41+
{
42+
return new self($name, null);
43+
}
44+
45+
public static function withContext(string $name, string $context): self
46+
{
47+
return new self($name, $context);
2748
}
2849
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Doctrine\DBAL\Tests\Types\Exception;
6+
7+
use Doctrine\DBAL\Types\Exception\UnknownColumnType;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class UnknownColumnTypeTest extends TestCase
11+
{
12+
public function testNew(): void
13+
{
14+
$exception = UnknownColumnType::new('custom_type');
15+
16+
self::assertSame('custom_type', $exception->getType());
17+
self::assertStringContainsString(
18+
'Unknown column type "custom_type" requested.',
19+
$exception->getMessage(),
20+
);
21+
}
22+
23+
public function testWithContext(): void
24+
{
25+
$exception = UnknownColumnType::withContext('custom_type', 'table "some_table"');
26+
27+
self::assertSame('custom_type', $exception->getType());
28+
self::assertStringContainsString(
29+
'Unknown column type "custom_type" requested for table "some_table".',
30+
$exception->getMessage(),
31+
);
32+
}
33+
}

0 commit comments

Comments
 (0)