Error Handler
From Harmoni
As of Harmoni 0.11.0, the ErrorHandler service has been removed and replaced with PHP5 exceptions.
Please see the HarmoniErrorHandler documentation for the new system.
To provide backward compatability with existing Harmoni code, the throwError($error) function has been retained and the Error class has be re-implemented to extend Exception. Calls to throwError(new Error($message, $service)); now just throw an exception with the message specified.
Contents |
Legacy ErrorHandler Documentation
Included in Harmoni is the ErrorHandler class. Like the DebugHandler, it stores messages in a queue and outputs them when you tell it to. However, some errors are fatal, and some are not. Fatal errors are printed immediately, along with any other errors in the queue.
Two services are registered using the ErrorHandler: ErrorHandler and UserError. The former acts as a "system" error manager, handling mostly fatal errors or warnings that are produced by your program and must be fixed by either an administrator or the programmer. The latter is meant to keep track of errors created by an end user, such as entering a malformatted email in a form field. Like the Debug service, there are wrapper functions for both of the error services. They will be explained at the end of this page.
Throwing a new error
There are two ways, using the ErrorHandler object, to add new errors. Both are outlined in the example:
$errHandler =& Services::getService("ErrorHandler"); // or, "UserError"
// now, throw a new error
$errHandler->addNewError("This is my error string!","category",false);
// or, make an Error object (you could use your own custom object)
$errHandler->addError(
new Error("This is my error string!","category",false)
);
Both of the above methods do exactly the same thing. The advantage of using the second would be to create your own custom Error classes, for example to output common text with only one changing parameter (like, new EmptyFieldError("myFieldName") would print "You forgot to enter required information for myFieldName.").
The arguments: 1 = the error string, 2 = a category (whatever you want), 3 = is the error fatal? (fatal errors stop program execution)
Wrapper functions throwError and userError
Provided are two error handler wrapper functions. Both work identically but on different services, mentioned above. throwError() will throw an error to the ErrorHandler Serivce, whereas userError() will throw to UserError.
// throw an error to ErrorHandler
throwError( new Error("AAAAAAH! Something went very wrong! Aborting...","DB-API",true) );
// or to UserError
userError( new Error("You didn't enter your name in the form!","user",false) );
Printing Errors
There are a couple ways to print errors from an ErrorHandler. 1) You can use one of the provided: ErrorPrinterBasic or SimpleHTMLErrorPrinter printer classes, or 2) you can write your own printer following the interface found in harmoni/core/errorHandler/errorPrinter.interface.php. All ErrorPrinters would be used in the same fashion:
$errHandler =& Services::getService("ErrorHandler"); // or, "UserError"
// now print the errors in HTML (or whatever)
$errHandler->printErrorsWithErrorPrinter( new ErrorPrinterBasic() );
// or, new SimpleHTMLErrorPrinter()
It's really that easy. Take a look at the PHPDoc for the ErrorHandler and ErrorPrinterInterface for more detailed information.
Categories: Harmoni | Manual | Services

