aka, Seeking generic Error Handler (ΟΚ to use commercially)
I doubt that I am the best PHP programmer around, so, although I have my own generic error handler for set_error_handler(), I wondered what others do and if there is a “best” (sorry if that sounds subjective – I just want to draw out general approaches (but even the ‘best practices’ tag has been removed from SO)).
To be objective about it, here’s what I think is needed. Please correct me if I am wrong & point me to some good code if you agree.
-
I want to capture as much information as possible – without knowing what the error was.
-
so, for instance, it makes sense to dump the call stack.
-
and
$_GET,$_POSTand$_SESSION. -
and I want the call stack & Globals to be pretty-printed
-
I want some ‘plain-text’ layout, not CSS & fancy JS to expand/collapse the information. My users may have to cut/paste into email or even print out & fax.
-
I would like to be able to add a header of my own devising, preferably as a parameter, but I can hack the code if need be. The header might include the program version, timestamp, etc (and, in my case, I have an audit track, so I can include the user’s last few actions, which led to the crash).
-
some users may allow my code to auto-email the report, some may wish to preview it forst & them email it and some may not want me to send email.
I suggest to go the “Exceptions” way.
Throw exceptions when there’s a user error, and you can convert php errors into exceptions, like this:
Albeit that this kind of behaviour works best in an OOP kind of environment. If you don’t have a single point of entry (like a frontcontroller), you may also catch loose exceptions with this:
Simple debugging with exceptions would go something a bit like this:
From there evaluating globals etc. is a walk in the park. You might look for inspiration to the Symfony Framework Debug Toolbar, which offers many of these requests.