dbal icon indicating copy to clipboard operation
dbal copied to clipboard

Unspecific exception text on unknown type

Open speller opened this issue 9 months ago • 3 comments

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,

speller avatar Apr 16 '25 01:04 speller

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.

greg0ire avatar Apr 16 '25 18:04 greg0ire

The stacktrace is the following:

Image

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;
    }

speller avatar Apr 18 '25 02:04 speller

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.

greg0ire avatar Apr 18 '25 06:04 greg0ire