Unspecific exception text on unknown type
Bug Report
| Q | A |
|---|---|
| Version | 4.2.3 |
Summary
Unspecific exception text on unknown type used in field definition.
Current behavior
Exception message:
Unknown column type "int" 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
Expected behavior
Same detailed explanation, but also specifying the erroneous class and field name.
How to reproduce
#[ORM\Column(type: 'int')]
public int $foo,
Please provide a stack trace, so it's possible to find the code that throws this exception. Also, you're reporting this on the DBAL repo, but the reproducer uses ORM code. The DBAL does not know about the ORM and therefore cannot provide a class and field name.
The stacktrace is the following:
There's no userland code in it. I'm reporting it here because the exception is in the DBAL component.
I think the exception can be handled in the \Doctrine\DBAL\Schema\Table::addColumn() method where both the table and column names are known so they can be added to the exception message and help users identify the error location more quickly. For example, something like this:
Error in table.column: Unknown column type "int" requested. Any Doctrine type that you use...
Maybe something like that code:
public function addColumn(string $name, string $typeName, array $options = []): Column
{
try {
$column = new Column($name, Type::getType($typeName), $options);
} catch (\Exception $e) {
throw new \RuntimeException("Error in {$this->getName()}.{$name}: " . $e->getMessage(), previous: $e);
}
$this->_addColumn($column);
return $column;
}
Handling it at the DBAL level is a good idea, because it should be good enough for ORM users, and will benefit people that use the DBAL but not the ORM. Please send a PR.