Convert Doctrine\DBAL\Exception to an interface #5726
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, the base exception being a class doesn't allow to reuse of the semantics of the SPL exception classes. For instance, distinguish between logic and runtime exceptions.
Not being able to reuse the semantics defined in SPL causes the following issue about documenting exceptions. For example, take the
NotSupported
exception which has the following ancestry:NotSupported → \Doctrine\DBAL\Exception → \Exception
. As static analyzers see it, semantically, it's just a generic exception as it extends the generic\Exception
class. But in fact, it identifies an error in the logic of the code that consumes the API and thereby should not be handled by the caller (an unchecked exception).Now, take this use case for example:
dbal/src/Platforms/AbstractPlatform.php
Lines 2012 to 2016 in 4bd24c8
The method is declared as throwing an exception with unknown semantics. As a result, static analyzers like PhpStorm flag the calls that don't handle this exception as incorrect. If we remove the
@throws
annotation, then thethrow
statement is flagged as incorrect. The only way to write statically correct code against this API is to catch the exception and do nothing with it, which is still wrong.Converting the class to an interface allows retaining the relation of the exception to the DBAL and at the same time reusing the semantics defined in SPL. For example:
dbal/src/Exception/InvalidArgumentException.php
Lines 7 to 14 in 4bd24c8
In subsequent patches, we can fine-tune the semantics of other exceptions (firstly,
NotSupported
) by changing their parent from\Exception
to something more specific.