Exceptables make exceptions exceptional. Exceptables provide some nice utility methods, but the main benefit is having a way to conveniently and quickly organize all the error cases in your application. Exceptables are easy to create and pass details to. They provide access to error info for both humans and code. Exceptables make it easy to extend, add, and maintain error handling code as your application grows.
Requires php 7.0 or later.
Recommended installation method is via Composer: simply composer require php-enspired/exceptable
.
<?php
use at\exceptable\Handler;
use at\exceptable\Exception as Exceptable;
// a simple Exceptable just for you
class FooException extends Exceptable {
const UNKNOWN_FOO = 1;
const INFO = [
self::UNKNOWN_FOO => [
'message' => 'unknown foo',
'tr_message' => "i don't know who, you think is foo, but it's not {foo}"
]
];
}
throw new FooException(FooException::UNKNOWN_FOO);
// on your screen:
// Fatal error: Uncaught FooException: unknown foo in ...
$handler = new Handler();
$handler
->onException(function($e) { error_log($e->getMessage()); return true; })
->register();
$context = ['foo' => 'foobedobedoo'];
throw new FooException(FooException::UNKNOWN_FOO, $context);
// in your error log:
// i don't know who, you think is foo, but it's not foobedobedoo
see more in the wiki.
I'm on Freenode at #php-enspired
, or open an issue on github. Feedback is welcomed as well.