Skip to content

Commit 6c06126

Browse files
committed
Adding context information when requesting invalid column type
1 parent acb68b3 commit 6c06126

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
@@ -11,12 +11,14 @@
1111
use Doctrine\DBAL\Platforms\Exception\NotSupported;
1212
use Doctrine\DBAL\Result;
1313
use Doctrine\DBAL\Schema\Exception\TableDoesNotExist;
14+
use Doctrine\DBAL\Types\Exception\UnknownColumnType;
1415

1516
use function array_filter;
1617
use function array_intersect;
1718
use function array_map;
1819
use function array_values;
1920
use function count;
21+
use function sprintf;
2022
use function strtolower;
2123

2224
/**
@@ -667,7 +669,11 @@ protected function _getPortableTableColumnList(string $table, string $database,
667669
{
668670
$list = [];
669671
foreach ($tableColumns as $tableColumn) {
670-
$column = $this->_getPortableTableColumnDefinition($tableColumn);
672+
try {
673+
$column = $this->_getPortableTableColumnDefinition($tableColumn);
674+
} catch (UnknownColumnType $unknownTypeException) {
675+
throw UnknownColumnType::withContext($unknownTypeException->getType(), sprintf('table %s', $table));
676+
}
671677

672678
$name = strtolower($column->getQuotedName($this->platform));
673679
$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)