A simple PHP trait which makes creating static constructors for exceptions nicer.
$ composer require tomphp/exception-constructor-tools
Define your exception:
<?php
use TomPHP\ExceptionConstructorTools\ExceptionConstructorTools;
class MyExceptionClass extends \RuntimeException
{
use ExceptionConstructorTools;
public static function forEntity($entity)
{
return self::create(
'There was an error with an entity of type %s with value of %s.',
[
self::typeToString($entity)
self::valueToString($entity)
]
);
}
}
Throw your exception:
if ($errorOccurred) {
throw MyExceptionClass::forEntity($entity);
}