Localized error reporting
Posted: Sun Jun 24, 2007 9:54 pm
I've implemented a little optional, localized error reporter, and I'd like some comments on the general interface, because once I commit fully into this design, there will be calls to this reporter everywhere in my application.
A few notes:
* The error collector is optional, so if it's disabled, the system should not attempt to generate errors
* The error collector is localized, so message keys need to be translated into full messages
* The error collector is separate from PHP's built-in error system. I consider those for developers: these are for end-users
So, as you may have guessed, an ErrorCollector is passed around the system. When an error is triggered, the following code is called:
$e is the error collector object (it's only called if it exists), and there are 2+ parameters:
1. Error severity, use same constants as PHP
2. Message key
3 and beyond. Message parameters
The error collector then consults the locale object, and retrieves the message (possibly with parameter substitution done to it). Then, the error collector consults a "global" context object, which contains information about the current line, current tag, etc. that was happening when the error happened, and performs some more substitution on the message. The fully-formed message is then saved in the error collector's array, and the execution chugs on.
A message that needs substitution looks something like 'This $1 and $2'; if an associative array is passed as a message parameter then you can get a bit fancier: 'This $item1 and $item2'.
To test error reporting, mock the error collector object and make expectations (you'll need one mock for each expectation).
Comments? I can show code, but hopefully this summary is more readable.
A few notes:
* The error collector is optional, so if it's disabled, the system should not attempt to generate errors
* The error collector is localized, so message keys need to be translated into full messages
* The error collector is separate from PHP's built-in error system. I consider those for developers: these are for end-users
So, as you may have guessed, an ErrorCollector is passed around the system. When an error is triggered, the following code is called:
Code: Select all
if ($e) $e->send(E_ERROR, 'ClassName: Message key', 'message-parameter-1');1. Error severity, use same constants as PHP
2. Message key
3 and beyond. Message parameters
The error collector then consults the locale object, and retrieves the message (possibly with parameter substitution done to it). Then, the error collector consults a "global" context object, which contains information about the current line, current tag, etc. that was happening when the error happened, and performs some more substitution on the message. The fully-formed message is then saved in the error collector's array, and the execution chugs on.
A message that needs substitution looks something like 'This $1 and $2'; if an associative array is passed as a message parameter then you can get a bit fancier: 'This $item1 and $item2'.
To test error reporting, mock the error collector object and make expectations (you'll need one mock for each expectation).
Comments? I can show code, but hopefully this summary is more readable.