jasny/error-handler

This package is abandoned and no longer maintained. No replacement package was suggested.

Error handler with PSR-7 support

v0.2.0 2017-01-25 01:27 UTC

This package is auto-updated.

Last update: 2019-12-02 14:17:59 UTC


README

Build Status Scrutinizer Code Quality Code Coverage SensioLabsInsight Packagist Stable Version Packagist License

Error handler with PSR-7 support.

Installation

The Jasny Error Handler package is available on packagist. Install it using composer:

composer require jasny/error-handler

Usage

$errorHandler = new Jasny\ErrorHandler();

Just creating an error handler will do nothing. You can use it for logging, handling fatal errors and as PSR-7 compatible middleware.

Logging

By default the error handler with only catch Throwables and not set the php error handler.

To log errors, set the logger using setLogger(). You can log with any PSR-3 compatible logger like Monolog.

The logUncaught() method will set the error handler, so warnings and notices can be logged. It may also register a shutdown function to handle uncatchable fatal errors.

use Monolog\Logger;
use Monolog\Handler\StreamHandler;

$errorHandler = new Jasny\ErrorHandler();

$log = new Logger('test');
$log->pushHandler(new StreamHandler('path/to/your.log'));

// Log fatal errors, warnings and uncaught exceptions
$errorHandler->setLogger($log);

$errorHandler->logUncaught(E_PARSE | E_ERROR | E_WARNING | E_USER_WARNING);
$errorHandler->logUncaught(Exception::class);
$errorHandler->logUncaught(Error::class); // PHP7 only

PSR-7 compatible middleware

The error handler can be used as PSR-7 compatible (double-pass) middleware.

The error will catch Exceptions and Errors.

You can use this middleware with:

For example use it with Relay:

use Relay\RelayBuilder;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;

$errorHandler = new Jasny\ErrorHandler();

$relay = new RelayBuilder();
$dispatcher = $relay->newInstance([$errorHandler->asMiddleware()]);

$response = $dispatcher((new ServerRequest())->withGlobalEnvironment(), new Response());

Or with Jasny Router:

use Jasny\Router;
use Jasny\Router\Routes\Glob as Routes;
use Jasny\HttpMessage\ServerRequest;
use Jasny\HttpMessage\Response;

$router = new Router(new Routes(['/**' => ['controller' => '$1', 'id' => '$2']));

$errorHandler = new Jasny\ErrorHandler();
$router->add($errorHandler->asMiddleware());

$response = $dispatcher((new ServerRequest())->withGlobalEnvironment(), new Response());

PHP 5 support

With PHP 5 errors aren't thrown, so the middleware won't handle it. To add middleware support for errors in PHP5, you should call converErrorsToExceptions(). This method will convert an error to an ErrorException.

Handling fatal errors

Errors that are not thrown, like syntax errors, are not caught and will cause a fatal error. With the logUncaught() method, you can specify that the error handler should also these kind of errors.

With the onFatalError() method you take additional action, like output a pretty error message.

ob_start();

$errorHandler = new Jasny\ErrorHandler();

$errorHandler->logUncaught(E_ERROR | E_RECOVERABLE_ERROR | E_USER_ERROR);

$errorHandler->onFatalError(function() {
    http_response_code(500);
    header('Content-Type: text/html');
    echo "<h1>An unexpected error occured</h1><p>The error has been logged.</p>";
}, true);

Use true as second argument of onFatalError to the output buffer before calling your function.

Combine with other error handlers

Using the error logger might lose backtrace information that other error handlers can pick up. Jasny Error Handler will always call the previous error handler, including the PHP internal error handler for non-thrown errors.

When using Rollbar you should not use the Rollbar handler for Monolog. By using Rollbar's own error handler, you'll get better error reports:

use Jasny\ErrorHandler;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

// Rollbar error handler will log uncaught errors
Rollbar::init(array('access_token' => 'POST_SERVER_ITEM_ACCESS_TOKEN'));

$log = new Logger('test');
$log->pushHandler(new RollbarHandler(Rollbar::$instance));

$errorHandler = new ErrorHandler();

// Jasny error handler will only log caught errors
$errorHandler->setLogger($log);