-
Notifications
You must be signed in to change notification settings - Fork 6
Error & Exception Handling
Using the Atsumi error & exception handling systems you can monitor all errors, exceptions & unexpected behaviour on your application as well as make sure you recover from the problem gracefully.
Atusmi converts errors in to ErrorException. Multiple listeners can be attached to the exception handler as well as a single recoverer. On the event of an exception the listeners are fired, these can do anything from write the full exception output to a log file, email it to the project maintainers or store it in a database. The recoverer will then fire which could display a fail whale, or take them back to their previous page and pop up a message explaining that a problem was encountererd. The Exception handler has built in flood control to make sure that a reoccuring problem (ie: database down) doesn’t bombard your listers over and over again.
Below is a simple example of how to configure the atsumi error handler. Usually a developer will have a method in there settings file such as configureErrorHandler() or similar which they call during settings __construct(). In most projects it’s useful to have a different exception handler configuration for your production project than those in your development project.
/* configure flood control */
Atsumi::error__setFloodControl($this->init_memcache, 60 * 15);
/* attach listeners */
Atsumi::error__addObserver (
new listener_LogToFile($this->get_senchaLogPath),
atsumi_ErrorHandler::EVENT_EXCEPTION_FC
);
Atsumi::error__addObserver (
new listener_EmailNotification($from, $fromName, $to, $toName),
atsumi_ErrorHandler::EVENT_EXCEPTION_FC
);
/* set recoverer to simply redirect */
Atsumi::error__setRecoverer(new recoverer_Redirect('/error/'));
Atsumi::error__setRecoverer(new recoverer_DisplayAndExit());
Sometimes you wan’t to control the recovery in a specific way, or surpress the exception but don’t want it to go un-noticed, for this you can listen() to the exception.
Atsumi::error__listen(Exception $e);
You feed this your caught exception as a parameter and this cycles through your defined listeners and fires each one, but then just returns back to you rather than activating your defined recoverer.
This can be used in a variety of scenarios:
- When working with third-party API’s you can suppress internal exceptions, listen to them for your debugging, but always return a generic error message (in less code than writing a specific recoverer).
- If the exception is occurring in none essential part of your code the user never has to even know there was a problem
- You never have to ignore an exception if allowing it to bubble would critically damage core functionality in your app.