2017 © Pedro Peláez
 

library asplode

Drop-in exception-based error handling for PHP.

image

eloquent/asplode

Drop-in exception-based error handling for PHP.

  • Tuesday, April 19, 2016
  • by ezzatron
  • Repository
  • 2 Watchers
  • 38 Stars
  • 29,144 Installations
  • PHP
  • 17 Dependents
  • 9 Suggesters
  • 3 Forks
  • 2 Open issues
  • 16 Versions
  • 5 % Grown

The README.md

Asplode

Drop-in exception-based error handling for PHP., (*1)

![Current version image][version-image] Current build status image ![Current coverage status image][coverage-image], (*2)

Installation and documentation

Usage

The Asplode error handler can be installed with a single statement:, (*3)

Eloquent\Asplode\Asplode::install();

What does Asplode do?

Asplode is a very simple PHP error handler implementation that throws ErrorException exceptions instead of using the default PHP error handling behaviour. This means that all non-fatal runtime errors are presented to the developer in the form of an exception. It also means that any unhandled errors are delivered to a single point: the global exception handler., (*4)

Why use Asplode?

Developers need the ability to decide how their code behaves when an error occurs. Exceptions offer the only truly consistent way to report and recover from errors in PHP., (*5)

This method of handling errors has proven to be extremely effective. Similar strategies are used in major PHP frameworks such as Laravel. Asplode is a standalone implementation that can be used for any project., (*6)

Fatal error handling

While it's not feasible to recover from fatal PHP errors, it is possible to report fatal errors in the same manner as uncaught exceptions., (*7)

With Asplode, fatal errors cause a synthesized exception representing the fatal error to be passed to the global exception handler. This allows developers to gracefully inform the user of fatal errors just before the PHP interpreter is shut down., (*8)

The Asplode fatal error handler is installed by default, but is only activated if a global exception handler is installed., (*9)

set_exception_handler(
    function (Exception $e) {
        echo $e->getMessage();
    }
);

Eloquent\Asplode\Asplode::install();

To use Asplode without the fatal error handler, use Asplode::installErrorHandler() instead of Asplode::install(). To use only the fatal error handler, use Asplode::installFatalHandler()., (*10)

Note that attempting to autoload files in the shutdown phase of PHP may be probelmatic; and as such, custom exception handlers should explicitly load their dependencies where possible., (*11)

Asserting that the current error handler is compatible

Code that assumes the use of Asplode may not work as expected unless the right type of error handler is installed. For example, code expecting to catch an ErrorException on failure will have unpredictable results if the installed error handler does not throw ErrorException instances., (*12)

To ensure that a correctly configured error handler is installed, Asplode provides the Asplode::assertCompatibleHandler() method., (*13)

use Eloquent\Asplode\Asplode;
use Eloquent\Asplode\Exception\ErrorHandlingConfigurationException;

try {
    Asplode::assertCompatibleHandler();
} catch (ErrorHandlingConfigurationException $e) {
    // handle appropriately
}

A compatible error handler is any handler that throws ErrorException exceptions. It does not need to be the implementation provided by Asplode., (*14)

Managing PHP's handler stacks

PHP's error and exception handlers approximate the behaviour of a stack. However, the interface for manipulating the stack is limited, and quite frankly, poorly implemented., (*15)

Asplode includes two classes to aid in management of these stacks, ErrorHandlerStack and ExceptionHandlerStack. Both implement HandlerStackInterface which provides a familiar interface for working with stacks. These classes do not require the use of the Asplode handler; they can be used in a standalone manner to manage the handler stacks., (*16)

Migrating existing code to work with Asplode

When the Asplode error handler is installed, the error_reporting setting will no longer have any effect. Notices, warnings, and errors will all result in an exception being thrown. Deprecation notices will not throw an exception, but will still be logged provided that PHP is configured to do so., (*17)

Code that has been written to handle legacy-style PHP errors will most likely need to be re-written. As an example, this type of logic:, (*18)

$fp = fopen('/path/to/foo', 'r'); // this throws a PHP warning if the file is not found

if ($fp === false) {
  // handle error opening file
}

would need to be rewritten to to handle exceptions:, (*19)

try {
  $fp = fopen('/path/to/foo', 'r');
} catch (ErrorException $e) {
  // handle error opening file
}

It's important to note that PHP can be very inconsistent in the way it reports error conditions. Some functions will return a boolean false to indicate an error has occurred; others may require the developer to call additional functions to check for errors; and others still may exhibit entirely non-standard behaviour., (*20)

Asplode does not free the developer from the responsibility of reading the PHP documentation, or making sure that they account for all possible error conditions., (*21)

Executing legacy code

Sometimes working with code that uses bad practices is unavoidable. A legacy PHP library might be perfectly functional and useful, but it may not anticipate exceptions being thrown when an error occurs., (*22)

Asplode's exception and error handler stacks both implement an executeWith() method that allows code to be executed with a different handler than the one currently installed. This method pops all current handlers off the stack temporarily, installs the specified handler (if one is provided) and executes the supplied callback. The original handler is restored after the callback is executed., (*23)

use Eloquent\Asplode\HandlerStack\ErrorHandlerStack;

$stack = new ErrorHandlerStack;

$result = $stack->executeWith(
    function () {
        // this code will be executed under the default handler
    }
);

$result = $stack->executeWith(
    function () {
        // this code will be executed under the supplied handler
    },
    'errorHandlerFunctionName'
);

$result = $stack->executeWith(
    function () {
        // this code will be executed under the supplied handler
    },
    function ($severity, $message, $path, $lineNumber) {
        // handle the error
    }
);

The Versions

19/04 2016

dev-develop

dev-develop https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

19/04 2016

dev-master

9999999-dev https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

19/04 2016

2.2.0

2.2.0.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

18/03 2016

dev-fix-hhvm-failures

dev-fix-hhvm-failures https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

11/12 2015

2.1.0

2.1.0.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

29/10 2014

2.0.1

2.0.1.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

04/03 2014

dev-feature/psr-conformance

dev-feature/psr-conformance https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

25/01 2014

2.0.0

2.0.0.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

01/08 2013

1.1.1

1.1.1.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

11/06 2013

1.1.0

1.1.0.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

04/03 2013

1.0.5

1.0.5.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

06/08 2012

1.0.4

1.0.4.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

 

The Development Requires

exception error handler handling failure errorexception fault

02/08 2012

1.0.3

1.0.3.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • icecavestudios/isolator 1.*

 

The Development Requires

exception error handler handling failure errorexception fault

11/07 2012

1.0.2

1.0.2.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • icecavestudios/isolator 1.*

 

The Development Requires

exception error handler handling failure errorexception fault

05/07 2012

1.0.1

1.0.1.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • icecavestudios/isolator *@dev

 

The Development Requires

exception error handler handling failure errorexception fault

02/07 2012

1.0.0

1.0.0.0 https://github.com/eloquent/asplode

Drop-in exception-based error handling for PHP.

  Sources   Download

MIT

The Requires

  • php >=5.3.0
  • icecavestudios/isolator *

 

The Development Requires

exception error handler handling failure errorexception fault