Skip to content

Conversation

mamazu
Copy link

@mamazu mamazu commented Jul 1, 2025

Q A
Type feature
Fixed issues closes #7012

Summary

Adding a way to set context on the UnkownColumnType. However in the place where I've added it there is no easy way to also get the name of the column, so I'm just providing the table, which should also already help.

@mamazu mamazu force-pushed the better_error_messages branch from ed91f05 to 6c06126 Compare July 1, 2025 15:49
try {
$column = $this->_getPortableTableColumnDefinition($tableColumn);
} catch (UnknownColumnType $unknownTypeException) {
throw UnknownColumnType::withContext($unknownTypeException->getType(), sprintf('table %s', $table));
Copy link
Member

@morozov morozov Jul 4, 2025

Choose a reason for hiding this comment

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

I think this is overcomplication. Instead, we could try reusing UnsupportedTableDefinition and adding another static constructor (e.g. fromIntrospectionException(). It would accept the table name and reference the exception caught from _getPortableTableColumnDefinition() as the previous exception.

This way, we will be able to supply any introspection exception with the context (table name) w/o introducing new classes.

Copy link
Author

Choose a reason for hiding this comment

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

Sounds like a reasonable idea but what is this UnsupportedTableDefinition class? I can't find it in the source code or the dependencies.

Copy link
Member

@morozov morozov Jul 4, 2025

Choose a reason for hiding this comment

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

Sorry for the confusion. This class is available only in 5.0.x (introduced in #6853).

Please feel free to re-introduce it in your PR (w/o the methods that exist in 5.0.x, only with yours).

Note that the 4.2.x branch accepts only bug fixes. An improvement like this needs to be targeted at 4.3.x.

Copy link
Author

Choose a reason for hiding this comment

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

Hm thinking about this, this change might be better suited for 5.0 anyway then seeing that using a different exception would also change the type of exception thrown in the method. However, I think it still would be cool to have it in 4.3.x without any bc breaks.

How do other parts of the application handle those kinds of things. Like adding context to an exception sounds like a common enough use-case.

@mamazu mamazu changed the base branch from 4.2.x to 4.3.x July 5, 2025 09:47
Copy link
Member

@derrabus derrabus left a comment

Choose a reason for hiding this comment

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

The motivation behind the change looks reasonable to me. However, there's no test that makes sure that the exception is raised with the correct message. The test that has been added is only a unit test for the exception class itself which is not sufficient.

Comment on lines 14 to 19
private function __construct(
private string $requestedType,
?string $context,
int $code = 0,
?Throwable $throwable = null,
) {
Copy link
Member

Choose a reason for hiding this comment

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

Changing the constructor signature or the constructor's visibility is a breaking change because we did not declare the constructor as internal. The named constructor new() took care of rendering the message which was fine. Please keep it that way. If you need a different message, feel free to add a second named constructor.

Copy link
Author

Choose a reason for hiding this comment

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

That sounds like a good approach. I've refactored it to a new method. I am not sure if the ensuing code duplication is something that could be easily factored out or if it is okay.

Copy link
Author

Choose a reason for hiding this comment

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

And a question about the tests. I couldn't find any tests for the abstract schema manager. Could you give me a lead on where I would need to add this?

Copy link
Member

Choose a reason for hiding this comment

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

What about SchemaManagerFunctionalTestCase?

Copy link
Author

Choose a reason for hiding this comment

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

Okay, I tried adding a test case but the problem is that it already fails on creation of the schema and it doesn't get to listing it. So when creating a column with an invalid type it throws an exception (but without context, as it doesn't know the table context yet).

I've added a test case in the column editor that it should throw an "Unknown column type" exception, is that enough or do I need to dig in how to create an invalid schema without the Table::editor()? Just to have it crash later when listing the schema.

Copy link
Member

Choose a reason for hiding this comment

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

I've added a test case in the column editor that it should throw an "Unknown column type" exception

That test would pass without your modifications already, wouldn't it? This basically means that your changes to the schema manager are still not covered.

Copy link
Author

Choose a reason for hiding this comment

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

Correct, this test already would have worked without my changes.

The problem I see is the test would fail when creating the column. Before I can assign it to any table to trigger the extended error message.

@mamazu mamazu force-pushed the better_error_messages branch from 30d5dee to 9caaba3 Compare August 17, 2025 21:49
Comment on lines 818 to 819
} 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.

@mamazu mamazu force-pushed the better_error_messages branch from f85c621 to 252b19a Compare August 17, 2025 22:24
@mamazu mamazu force-pushed the better_error_messages branch from 252b19a to 92d7814 Compare August 17, 2025 22:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Show column name when column type is broken
3 participants