A simple and flexible way to handle and manage errors and exceptions in your PHP application.
- PHP errors are turned into exceptions by default.
- You can register multiple closures to handle your errors.
- No other dependencies - small size - 1 file only.
$ composer require rammewerk/error-handler
The error handler should be registered early in your application to ensure they capture any issues that might occur from the outset. If registered late, initial errors might go uncaught. Early registration also promotes structured error management, facilitating easier debugging and maintenance.
use Rammewerk\Component\ErrorHandler;
$errorHandling = new ErrorHandler();
$errorHandling->log( static function (\Throwable $e) {
# Log your exceptions
});
$errorHandling->report( static function (\Throwable $e) {
# Show the exception to your user.
});
There are basically no difference between the log
and report
. Log closures will be called first, then the
reports. So you can add different log
handlers at later point in script, even though an earlier registered report
closure might be used to exit PHP.
log
closures are called in the order they got registered. reports
are called the in reverse order. So you can early
in your application add a general message and then later on add new reports to handle different scenarios the handler
calls the "general" error report.
PHP will either throw exceptions - which are intended to be caught - or print Errors which are generally unrecoverable.
But instead of having to deal with both exceptions and errors, we convert errors so you only have to deal
with exceptions. No more @file_get_contents
just nice and neat try/catch.
Uncaught exceptions will be given to your log
and report
closures.
Many other frameworks or libraries are way more advanced, and comes with loads of functions. This ErrorHandler is simple by design - you decide how to handle your uncaught exceptions and errors.
- Add the ErrorHandler class as early in your application as possible.
- Add
log
andreport
closure at any point you want. TheErrorHandler
will only call these closures if the exception occurred after the registered closure. So, consider addinglog
andreport
closures as soon as possible. - You can add multiple
log
andreport
closures.log
will be called beforereport
closures. - If you want to remove previous registered closures - set the second argument as true in
log
orreport
. Example:$error->log( $closure, true )
. This can be useful if you want to replace logging after calling other dependencies at a later point in your scripts.
use Rammewerk\Component\ErrorHandler;
CONST DEBUG_MODE = true;
$errorHandler = new ErrorHandler();
/** Log latest exception to JSON file */
$errorHandler->log( static function (\Throwable $e) {
file_put_contents( 'latest_error.json', json_encode($e->getMessage()) );
});
/** Show default 500 error page */
$errorHandler->report( static function () {
http_response_code( 500 );
echo file_get_contents( "errors/500.html" );
die;
} );
/**
* Show error details if DEBUG_MODE.
* Second argument here (true) is used to reset the report list.
* report closures
*/
if( DEBUG_MODE ) {
$errorHandler->report( function (\Throwable $e) {
$debug = new \CustomDebugClass();
$debug->exception($e);
die;
}, true);
}
report
and log
closures handle any Throwable
exceptions. To distinguish between exception types, incorporate
an instanceOf
check within the closure.
$errorHandling->report( function (\Throwable $e) {
if( $e instanceof \MyCustomException) ) {
die('MyCustomException was thrown');
}
} );