diff --git a/src/Schema/AbstractSchemaManager.php b/src/Schema/AbstractSchemaManager.php index b9afdb4ebef..e1c998d93f1 100644 --- a/src/Schema/AbstractSchemaManager.php +++ b/src/Schema/AbstractSchemaManager.php @@ -13,6 +13,7 @@ use Doctrine\DBAL\Schema\Exception\TableDoesNotExist; use Doctrine\DBAL\Schema\Name\Parsers; use Doctrine\DBAL\Types\Exception\TypesException; +use Doctrine\DBAL\Types\Exception\UnknownColumnType; use Doctrine\Deprecations\Deprecation; use Throwable; @@ -812,7 +813,15 @@ protected function _getPortableTableColumnList(string $table, string $database, { $list = []; foreach ($rows as $row) { - $column = $this->_getPortableTableColumnDefinition($row); + try { + $column = $this->_getPortableTableColumnDefinition($row); + } catch (UnknownColumnType $unknownTypeException) { + throw UnknownColumnType::newWithContext( + $unknownTypeException->getRequestedType(), + $table, + $unknownTypeException, + ); + } $name = strtolower($column->getQuotedName($this->platform)); $list[$name] = $column; diff --git a/src/Types/Exception/UnknownColumnType.php b/src/Types/Exception/UnknownColumnType.php index 5397f808b71..4624f21b0f6 100644 --- a/src/Types/Exception/UnknownColumnType.php +++ b/src/Types/Exception/UnknownColumnType.php @@ -5,16 +5,42 @@ namespace Doctrine\DBAL\Types\Exception; use Exception; +use Throwable; use function sprintf; final class UnknownColumnType extends Exception implements TypesException { + private string $requestedType; + + public function getRequestedType(): string + { + return $this->requestedType; + } + public static function new(string $name): self { - return new self( + $object = new self(sprintf( + 'Unknown column type "%s" requested. Any Doctrine type that you use has ' + . 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' + . 'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' + . 'introspection then you might have forgotten to register all database types for a Doctrine Type. ' + . 'Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement ' + . 'Type#getMappedDatabaseTypes(). If the type name is empty you might ' + . 'have a problem with the cache or forgot some mapping information.', + $name, + )); + + $object->requestedType = $name; + + return $object; + } + + public static function newWithContext(string $name, string $tableName, ?Throwable $previous): self + { + $object = new self( sprintf( - 'Unknown column type "%s" requested. Any Doctrine type that you use has ' + 'Unknown column type "%s" requested for table "%s". Any Doctrine type that you use has ' . 'to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the ' . 'known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database ' . 'introspection then you might have forgotten to register all database types for a Doctrine Type. ' @@ -22,7 +48,13 @@ public static function new(string $name): self . 'Type#getMappedDatabaseTypes(). If the type name is empty you might ' . 'have a problem with the cache or forgot some mapping information.', $name, + $tableName, ), + previous: $previous, ); + + $object->requestedType = $name; + + return $object; } } diff --git a/tests/Schema/ColumnEditorTest.php b/tests/Schema/ColumnEditorTest.php index 3ca067c412e..01635af44c7 100644 --- a/tests/Schema/ColumnEditorTest.php +++ b/tests/Schema/ColumnEditorTest.php @@ -7,6 +7,7 @@ use Doctrine\DBAL\Schema\Column; use Doctrine\DBAL\Schema\Exception\InvalidColumnDefinition; use Doctrine\DBAL\Schema\Name\UnqualifiedName; +use Doctrine\DBAL\Types\Exception\UnknownColumnType; use Doctrine\DBAL\Types\IntegerType; use Doctrine\DBAL\Types\Type; use Doctrine\DBAL\Types\Types; @@ -78,4 +79,17 @@ public function testTypeNotSet(): void $editor->create(); } + + public function testInvalidType(): void + { + $this->expectException(UnknownColumnType::class); + $this->expectExceptionMessage('Unknown column type "unknown_type"'); + + Column::editor() + ->setUnquotedName('column_char') + ->setTypeName('unknown_type') + ->setFixed(true) + ->setLength(2) + ->create(); + } } diff --git a/tests/Types/Exception/UnknownColumnTypeTest.php b/tests/Types/Exception/UnknownColumnTypeTest.php new file mode 100644 index 00000000000..3505cedb1c0 --- /dev/null +++ b/tests/Types/Exception/UnknownColumnTypeTest.php @@ -0,0 +1,33 @@ +getRequestedType()); + self::assertStringContainsString( + 'Unknown column type "custom_type" requested.', + $exception->getMessage(), + ); + } + + public function testWithContext(): void + { + $exception = UnknownColumnType::newWithContext('custom_type', 'some_table', null); + + self::assertSame('custom_type', $exception->getRequestedType()); + self::assertStringContainsString( + 'Unknown column type "custom_type" requested for table "some_table".', + $exception->getMessage(), + ); + } +}