Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
20.95k stars 1.9k forks source link

catching exceptions in exception handling #1858

Closed Henning256 closed 9 months ago

Henning256 commented 9 months ago

When an Exception is thrown while logging that has a non numberic code another exception is thrown while formating the exception. This is for example the case when logging to a database and a PDOException is thrown.

[object] (ErrorException(code: 0): Undefined property: Illuminate\Database\QueryException::$code

stof commented 9 months ago

$e->getCode is not supposed to throw an exception though. This should be reported to Laravel so that they fix their QueryException.

Henning256 commented 9 months ago

This is not a laravel problem. It is well known, that the PDOException which the QueryException extends causes this problem. Look for example in many user comments of the official PHP documentation for the PDOException: The PDOException class @ php.net

I already tried to handle it in laravels QueryException but it extends PDOException and PDOException::getCode() is final. There seems to be no known way to handle it in an extended PDOException. All solutions seem to base on the idea of catching the PDOException and raise a custom exception.

This all in mind it seems much more elegant to fix it here instead of everywhere a PDOException might occur.

stof commented 9 months ago

@Henning256 PDOException::getCode does not throw an exception (and especially not one saying Undefined property).

Henning256 commented 9 months ago

Thank you stof! I can not reproduce outside of LineFormater. Very strange.

Closing for further investigation.