2017 © Pedro Peláez
 

library php-error-handler

image

kegi/php-error-handler

  • Sunday, January 28, 2018
  • by kegi
  • Repository
  • 1 Watchers
  • 5 Stars
  • 32 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 8 Versions
  • 7 % Grown

The README.md

php-error-handler

PHP7; Object oriented error handler. This is an alpha version., (*1)

Introduction

This error handler detects PHP errors, convert them into exceptions and let you handle them easilly. Aditionnaly, this library handle the output buffering to avoid uncompleted body response. This library optionnaly log errors (accepts PSR Interface)., (*2)

Installation

Install this library with Composer, (*3)

composer require kegi/php-error-handler

Debug mode

disabled by default, (*4)

  • With debug enabled, errors are displayed normally (you are still notifided).
  • With debug disabled, all errors are hidden, you are notified and it's your job to return an error message.

Strict mode

disabled by default, (*5)

Strict mode will convert all non-fatal errors into fatal errors., (*6)

Complexe error handling runtime

You can execute complexe code if a fatal error is detected. (eg. you can call your errorController, build a response from a template according to the user's language settings...), (*7)

If an other fatal error occured while handling this fatal error... (eg. a fatal error on the core of your app), you'll be notified that an unrecoverable error occured and you'll be able to return an error message (eg. return the content of a static error html page)., (*8)

How to implement it on your project

1) you instanciate PhpErrorHandler at the very beggining of your project., (*9)

2) you provided a callable function for the wanted error level (non-fatal, fatal and unrecoverable)., (*10)

3) the uncaught exception of your project will naturally go on your fatal error method. You don't need to handle them differently anymore., (*11)

Example


<?php namespace Your\Project\Bootstrap; use KeGi\PhpErrorHandler\PhpErrorHandler; use KeGi\PhpErrorHandler\PhpFatalErrorException; class App { public function __construct() { /*instanciate the error handler*/ (new PhpErrorHandler()) ->setDebug(false) //prod #->setErrorCallback([$this, 'handleError']) //most projects don't need this ->setFatalErrorCallback([$this, 'handleFatalError']) ->setUnrecoverableErrorCallback([$this, 'handleUnrecoverableError']); } public function run() { //run your application... throw new \Exception('Uncaught exception'); //or... trigger_error('Uncaught error', E_USER_ERROR); } public function handleFatalError(PhpFatalErrorException $phpFatalErrorException) { // you can check the php error like this : if($phpFatalErrorException->getCode() === E_PARSE){ //parse error occured... } // you would normally call your router and emit a response return 'A fatal occured occured...'; } public function handleUnrecoverableError() { //you could include a static error page //eg. return include '/static/server-error.html'; return 'A very bad error occured'; } }

Command line (CLI)

The errors on command line will also be handled., (*12)

  • In case of a fatal error, the output buffer won't be clear in CLI. (There is no output buffer)
  • In case of a second level fatal error, the unrecoverable callback won't be used in CLI.

Unit testing

coming soon, (*13)

workflow

alt tag, (*14)

Output buffering

It's a good practice to build a response object and to emit it when ready, most php router do that. If your project generate content directly (eg. echo, print, var_dump...), this will also be handled., (*15)

If a fatal error occured with debug mode disabled, the content already generated will be dismiss so only our error response is visible., (*16)

On the error callbacks, you can echo directly your message or return a string., (*17)

Parameters

Constructor

If you don't want the library to changed php errors settings (ini_set and error_reporting), set $setErrorDisplay to false on the constructor., (*18)

__construct(
    bool $debug = false,
    bool $strict = false,
    $errorCallback = null,
    $fatalErrorCallback = null,
    $unrecoverableErrorCallback = null,
    $errorLogger = null,
    bool $setDisplayErrors = true
    )

Debug mode

Set/unset debug mode. (default: false), (*19)

setDebug(bool $debug)
hasDebug() : bool

Strict mode

Set/unset strict mode. With strict mode enabled, non-fatal php error (such as E_NOTICE) are converted into fatal error. (default: false), (*20)

setStrict(bool $strict)
isStrict() : bool

Error callback

Set/unset error callback. This will be call for every single php (non-fatal) error. Most project don't need this., (*21)

setErrorCallback([mixed $callable])
getErrorCallback() : mixed

Fatal error callback

Set/unset fatal error callback. This will be call in case of a fatal error. You can print or return your input., (*22)

setFatalErrorCallback([mixed $callable])
getFatalErrorCallback() : mixed

Unrecoverable error callback

Set/unset fatal error callback. This will be call in case of a fatal error from "fatalErrorCallback" or if don't have a fatal error callback defined. You can print or return your input., (*23)

setUnrecoverableErrorCallback([mixed $callable])
getUnrecoverableErrorCallback() : mixed

Error logger

Set/unset error logger. ErrorLogger need to implement PSR Logger interface. Note: Logs are enabled with or without debug mode., (*24)

setErrorLogger([LoggerInterface $errorLogger])
getErrorLogger() : mixed

Cancel

This will cancel the error handler. Once its cancelled, you can't re-enabled it. You'll still be able to read values of the handler (getters). Trying to edit value of the handler (setters) will throw an exception., (*25)

cancel()
isCancelled() : bool

The Versions

28/01 2018

dev-master

9999999-dev

  Sources   Download

The Requires

 

28/01 2018

0.1.6

0.1.6.0

  Sources   Download

The Requires

 

06/02 2017

0.1.5

0.1.5.0

  Sources   Download

The Requires

 

06/02 2017

0.1.4

0.1.4.0

  Sources   Download

The Requires

 

19/09 2016

0.1.3-a

0.1.3.0-alpha

  Sources   Download

The Requires

 

16/09 2016

0.1.2-a

0.1.2.0-alpha

  Sources   Download

The Requires

 

11/09 2016

0.1-a

0.1.0.0-alpha

  Sources   Download

The Requires

 

10/09 2016

dev-feature/unit-test-webserver

dev-feature/unit-test-webserver

  Sources   Download

The Requires

 

The Development Requires