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 tend to freak out when an error happens. This gets horrible in PHP context as the infra dribbles on top of plenty of moving parts, it is easy to get frustrated. When people get in that mood it gets complicated to carry on further communication. I figured out that I needed to create a really friendly error handling experience for everyone involved, especially me.
The error should be explicit, with a clear document hierarchy that anyone can understand, especially non-developers. I needed portability so peers could share, and it must be available in all contexts required by Chevereto (HTTP, CLI, log).
Chevere\Throwable\Exceptions\LogicException thrown
in /var/www/html/demo/throws/throws.php:17
RuntimeException thrown
Oops! in /var/www/html/demo/throws/throws.php:21
Message
Don’t panic this is just a drill.
Incident ID:627064085e6fc
Time
2022-05-02T23:06:48+00:00 [1651532808]
Stack trace
#0 /var/www/html/demo/throws/throws.php:17
{main}()
#1 /var/www/html/demo/throws/runtime.php:14
require()
#2 /var/www/html/demo/demo.php:32
require(string(length=68))
Server
Darwin rodolfo-mac.Home 21.4.0 Darwin Kernel Version 21.4.0: Fri Mar 18 00:47:26 PDT 2022; root:xnu-8020.101.4~15/RELEASE_ARM64_T8101 arm64
Features
There’s a strong focus on 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 you 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 throwables automatically. For example, to quickly 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);
For more details and source code you can visit Github.