2017 © Pedro Peláez
 

library cerberus

PHP Error handler

image

kslimani/cerberus

PHP Error handler

  • Monday, November 9, 2015
  • by kslimani
  • Repository
  • 1 Watchers
  • 2 Stars
  • 100 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 13 Versions
  • 5 % Grown

The README.md

Cerberus error handler

Simple PHP error handler., (*1)

Registering

Add in composer.json file :, (*2)

{
    "require": {
        "kslimani/cerberus": "~0.1.0"
    },
}

Usage

Quick setup :, (*3)


error_reporting(-1); use Cerberus\ErrorHandler; use Cerberus\Handler\DebugHandler; $errorHandler = new ErrorHandler; $errorHandler->addHandler(new DebugHandler);

Cerberus come with a PSR-3 Logger Interface error handler :, (*4)


use Cerberus\ErrorHandler; use Cerberus\Handler\LoggerHandler; use Monolog\Logger; use Monolog\Handler\StreamHandler; $errorHandler = new ErrorHandler; $logger = new Logger('errors'); $logger->pushHandler(new StreamHandler('/path/to/errors.log', Logger::NOTICE)); $errorHandler->addHandler(new LoggerHandler($logger));

Callable can also be added as handler :, (*5)


use Cerberus\ErrorHandler; $errorHandler = new ErrorHandler; $errorHandler->addHandler(function($message, $extra) { // $message is a formatted error message // $extra is an array with error/exception details });

Error/exception details

Details array content differ according to error type., (*6)

Error :, (*7)


[ 'displayType' : 'The error display type', 'context' : 'The application context', 'memory' : 'The memory peak usage, ONLY if debug is true', 'trace' : 'The error backtrace, ONLY if debug is true', 'type' : 'The error type, ONLY in CallableHandler', 'message' : 'The error message, ONLY in CallableHandler', 'file' : 'The error file, ONLY in CallableHandler', 'line' : 'The error line, ONLY in CallableHandler', ]

Exception :, (*8)


[ 'displayType' : 'The error display type', 'exception' : 'The exception object', 'memory' : 'The memory peak usage, ONLY if debug is true', 'code' : 'The http status code, ONLY if instance of HttpExceptionInterface', ]

Note : HttpExceptionInterface refer to Symfony\Component\HttpKernel\Exception\HttpExceptionInterface., (*9)

Error handler priority

Error handlers are ordered by priority (from higher to lower values)., (*10)

The first added handler get a priority value of 10, the second is 11, etc ..., (*11)

Priority can be changed with setPriority() method and must be set BEFORE adding handler., (*12)

  • DebugHandler default priority value is 0 (last handler)
  • LoggerHandler default priority value is 100 (first handler)
  • NewRelicHandler default priority value is 95 (after LoggerHandler)

Silex integration example

This Silex example application should handle all PHP errors :, (*13)


use Cerberus\ErrorHandler; use Cerberus\Handler\DebugHandler; use Cerberus\Handler\LoggerHandler; use Monolog\Logger; use Monolog\Handler\StreamHandler; use Silex\Application; use Silex\Provider\TwigServiceProvider; use Symfony\Component\HttpFoundation\Response; // Setup autoloader require_once '/path/to/vendor/autoload.php'; $debug = true; error_reporting(-1); ini_set('display_errors', 0); // Setup error handler $errorHandler = new ErrorHandler; $errorHandler ->setDebug($debug) ->setThrowExceptions(false) ->setThrowNonFatal(false) ->setCallPreviousErrorHandler(false) ->setCallPreviousExceptionHandler(false) ; if ($debug) { $errorHandler->addHandler(new DebugHandler(false)); } $logger = new Logger('errors'); $logger->pushHandler(new StreamHandler('/path/to/errors.log', Logger::NOTICE)); $errorHandler->addHandler(new LoggerHandler($logger)); // Create and setup application $app = new Application(); $app['debug'] = $debug; $app['exception_handler']->disable(); $app['cerberus'] = $errorHandler; // Register services $app->register(new TwigServiceProvider()); // Register simple error pages service $app['error.response'] = $app->protect(function ($code) use ($app) { if ($app->offsetExists('twig')) { // 404.html, or 40x.html, or 4xx.html, or default.html $templates = array( 'errors/'.$code.'.html', 'errors/'.substr($code, 0, 2).'x.html', 'errors/'.substr($code, 0, 1).'xx.html', 'errors/default.html' ); return new Response($app['twig']->resolveTemplate($templates)->render(array('code' => $code)), $code); } else { return new Response(sprintf("<h1>HTTP Error %s</h1>", $code), $code); } }); // Register fatal error handler $app['cerberus']->addHandler(function($message, $extra) use ($app) { if ($app['debug']) { return; } $app['cerberus']->emptyOutputBuffers(); $response = $app['error.response'](500); return $response->send(); }); // Register application exception handler $app->error(function (\Exception $e, $code) use ($app) { if (($code >= 500) && $app['debug']) { return; } return $app['error.response']($code); }); // Setup routes $app->get('/hello/{name}', function($name) use($app) { return 'Hello ' . $app->escape($name); }); // Run application $app->run();

The Versions

09/11 2015

dev-master

9999999-dev https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

09/11 2015

0.2.1

0.2.1.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

07/08 2015

0.2.0

0.2.0.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

16/03 2015

0.1.9

0.1.9.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

13/03 2015

0.1.8

0.1.8.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

13/03 2015

0.1.7

0.1.7.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

13/03 2015

0.1.6

0.1.6.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

13/03 2015

0.1.5

0.1.5.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

11/03 2015

0.1.4

0.1.4.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

29/09 2014

0.1.3

0.1.3.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

26/09 2014

0.1.2

0.1.2.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

23/09 2014

0.1.1

0.1.1.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

exception error handler cerberus

04/09 2014

0.1.0

0.1.0.0 https://github.com/kslimani/cerberus

PHP Error handler

  Sources   Download

MIT

The Requires

  • php >=5.5.0

 

The Development Requires

by Avatar kslimani

psr-3 silex symfony exception error handler cerberus httpkernel