Re: [RFC] Throwable Hierarchy Policy for Extensions
| From: | Tim Düsterhus | Date: | Sun, 27 Apr 2025 20:57:24 +0000 |
| Subject: | Re: [RFC] Throwable Hierarchy Policy for Extensions | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-127211@lists.php.net to get a copy of this message | ||
Hi
On 4/27/25 22:40, Kamil Tekiela wrote:
This is intended to say “if a user calls ->getMessage() and consumes it in anThe exception message MUST NOT be the only property that allows todifferentiate different types of error that the user may be interested in. What does this mean exactly? Can you give an example?
if() statement, then you are doing it wrong” and “the error message is intended for human consumption and changes to the message are not considered breaking changes”.
Ideally different exception classes should be used, but using the $code property to differentiate between different types of error is also acceptable when there is a wide range of errors the user might be interested in. For PDO this might be:
PdoException extends Exception
PdoError extends Error
UnsuccessfulQueryException extends PdoException
QuerySyntaxError extends PdoError
And then in UnsuccessfulQueryException, use a different code for "Duplicate entry", "Query timed out", and "Deadlock" to avoid adding a separate class for each failure case. It might make sense to add classes for the failure cases that are most likely to require special handling. e.g. a DeadlockException to retry the transaction.
But as a user I should not need to do:
again:
try {
$query->execute();
} catch (UnsuccessfulQueryException $e) {
if (str_contains($e->getMessage(), 'Timeout')) {
goto again;
}
throw $e
}
to determine whether it was a timeout, a deadlock or a duplicate entry.
Do you have a wording suggestion to make it clearer what is meant by that sentence?
Best regards
Tim Düsterhus