=============
Base Exception Library for PHP 8.3+ (The latest version: 5.1.0)
Missions:
- Additional structural data for exceptions.
- Aspects for exceptions.
- Aggregation exceptions within exceptions.
- Registration exceptions in the global registry for logging.
- Support for the concept of message templates.
- Support tags for exceptions (for elastic logging as example).
And most importantly: make it all easy and simple ;)
class MyException extends \Exceptions\BaseException
{
protectedstring$template = 'The template error message with {var}';
publicfunction__construct($var)
{
parent::__construct
([
'var' => $this->toString($var)
]);
}
}
$exception = newMyException('string');
// should be printed: The template error message with 'string'echo$exception->getMessage();use \Exceptions\Registry;
use \Exceptions\LoggableException;
Registry::resetExceptionLog();
$exception = newLoggableException('this is a loggable exception');
$log = Registry::getExceptionLog();
if($log[0] === $exception)
{
echo'this is loggable $exception';
}The basic use:
thrownewBaseException('message', 0, $previous);List of parameters:
// use array()$exception = newBaseException
([
'message' => 'message',
'code' => 0,
'previous' => $previous,
'mydata' => [1,2,3]
]);
...// print_r([1,2,3]);print_r($exception->getExceptionData());The processor for Monolog allows populating exception metadata in the log.
<?phpuseMonolog\Level;
useMonolog\Logger;
useMonolog\Handler\StreamHandler;
useIfCastle\Exceptions\LoggableException;
useIfCastle\Exceptions\MonologProcessor;
$log = newLogger('name');
$log->pushProcessor(newMonologProcessor());
// Put some records to the log$log->error(
'this is a loggable exception', ['exception' => newLoggableException('this is a loggable exception')]
);try
{
try
{
thrownew \Exception('test');
}
catch(\Exception$e)
{
// inherits data ExceptionthrownewBaseException($e);
}
}
catch(BaseException$exception)
{
// should be printed: "test"echo$exception->getMessage();
}The container is used to change the flag is_loggable:
try
{
try
{
// not loggable exception!thrownewBaseException('test');
}
catch(\Exception$e)
{
// log BaseException, but don't log LoggableExceptionthrownewLoggableException($e);
}
}
catch(LoggableException$exception)
{
// echo: "true"if($exception->getPrevious() === $e)
{
echo'true';
}
}try
{
dispatch_current_url();
}
catch(BaseException$myException)
{
$myException->appendData(['browser' => get_browser()]);
// and throw exception on...throw$myException;
}class ClassNotExist extends BaseException
{
// This exception will be loggedprotectedbool$isLoggable = true;
/** * ClassNotExist * * @param string $class Class name */publicfunction__construct(string$class)
{
parent::__construct
([
'template' => 'Сlass {class} does not exist',
'class' => $class
]);
}
}class MyFatalException extends BaseException
{
// This exception has aspect: "fatal"protectedbool$isFatal = true;
}class MyException extends BaseException
{
publicfunction__construct($object)
{
$this->setDebugData($object);
parent::__construct('its too bad!');
}
}