ThrowableHandler for PHP

Multi-purpose throwable handling.

rodber
rodber   GitHub

New from Chevere is the chevere/throwable-handler package for PHP. This software enables to configure a friendly error handling, with a myriad of alternatives to support generating documents for HTML, console and plain-text.

Error handling has been a nasty experience for me as users trend to freak out when error happens. This gets horrible in PHP context as the infra dribbles on top of plenty moving parts, it is easy to get frustrated. When people get in that mood it gets complicated to carry further communication. I figured out that I needed to create a really friendly error handling experience for everyone involved, specially me.

The error should be explicit, with a clear document hierarchy that anyone can understand, specially non-developers. I needed portability so peers can share, and it must be available in all contexts required by Chevereto (HTTP, CLI, log).

# Demo

HTML demo

# Features

There's a strong focus in consistency. All documents share the exact same text content, try copying the HTML text and it will match console and plain-text representations. This is because the system is intended to be used in contexts where users and/or developers need to share these documents.

The system enables to go all-auto or fine-tune everything, all depends on how your app works.

# Automatic handling

Using set_exception_handler this package can be used to handle throwable automatically. For example, to quick setup the console handler for the whole script:

use Chevere\ThrowableHandler\ThrowableHandler;

set_exception_handler(ThrowableHandler::CONSOLE);

# Trigger handling

You can use helper functions to manually setup throwable handling. For example, to handle a caught $e as HTML:

use function Chevere\ThrowableHandler\handleAsHtml;

try {
    // ...
} catch(Throwable $e) {
    handleAsHtml($e);
}

# Manual handling

You can do the whole thing all manual and retrieve $document directly. For example, a plain document:

use function Chevere\ThrowableHandler\plainDocument;

$document = plainDocument($throwable);

# Learn more

Head over to the documentation and leave a star at GitHub.

Rodolfo blogging since 2012.