The Error Library¶
The Error library provides handy utilities for error flow alteration.
Installation¶
1 | php composer.phar require "korowai/errorlib:dev-master"
|
Purpose¶
The main purpose of Error library is to provide handy utilities to control the flow of PHP errors within an application. It is designed to play nicely with our Context Library, so one can temporarily redirect PHP errors to custom handlers in one location without altering other parts of code.
Korowai Framework uses The Error Library to implement variants of PHP functions to throw predefined exceptions when the original functions trigger errors.
Basic Example¶
In the following example we’ll redirect errors from one invocation of a problematic function to a no-op error handler. The example uses the following functions
1 2 | use function Korowai\Lib\Context\with;
use function Korowai\Lib\Error\emptyErrorHandler;
|
and the problematic function is
1 2 3 4 | function trouble()
{
trigger_error('you have a problem');
}
|
The function could normally cause some noise. For example, it could call
default or an application-wide error handler. Invoking it with @
only
disables error messages, but the handler is still invoked. We can prevent this
by temporarily replacing original handler with our own empty handler. This is
easily achieved with Contexts and
EmptyErrorHandler
.
1 2 3 | with(emptyErrorHandler())(function ($eh) {
trouble();
});
|