Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -812,7 +813,11 @@ 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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$unknownTypeException and its stack trace are destroyed here. Please always attach the an exception as $previous when upcasting it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, fixed it.

}

$name = strtolower($column->getQuotedName($this->platform));
$list[$name] = $column;
Expand Down
40 changes: 34 additions & 6 deletions src/Types/Exception/UnknownColumnType.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,47 @@

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(
sprintf(
'Unknown column type "%s" requested. Any Doctrine type that you use has '
$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,
),
);
$name,
));

$object->requestedType = $name;

return $object;
}

public static function newWithContext(string $name, string $tableName): self
{
$object = new self(sprintf(
'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. '
. '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,
$tableName,
));

$object->requestedType = $name;

return $object;
}
}
33 changes: 33 additions & 0 deletions tests/Types/Exception/UnknownColumnTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Types\Exception;

use Doctrine\DBAL\Types\Exception\UnknownColumnType;
use PHPUnit\Framework\TestCase;

class UnknownColumnTypeTest extends TestCase
{
public function testNew(): void
{
$exception = UnknownColumnType::new('custom_type');

self::assertSame('custom_type', $exception->getRequestedType());
self::assertStringContainsString(
'Unknown column type "custom_type" requested.',
$exception->getMessage(),
);
}

public function testWithContext(): void
{
$exception = UnknownColumnType::newWithContext('custom_type', 'some_table');

self::assertSame('custom_type', $exception->getRequestedType());
self::assertStringContainsString(
'Unknown column type "custom_type" requested for table "some_table".',
$exception->getMessage(),
);
}
}