🧰 Provides a set of tools to handle exceptions in PHP applications.
-
Install via composer
composer require phphd/exception-toolkit
-
In case you are using symfony, enable the bundle in the
bundles.php
PhPhD\ExceptionToolkit\Bundle\PhdExceptionToolkitBundle::class => ['all' => true],
Allows you to unwrap composite exceptions and get the atomic errors you are interested in:
use PhPhD\ExceptionToolkit\Unwrapper\ExceptionUnwrapper;
/** @var ExceptionUnwrapper $unwrapper */
$unwrapper = getUnwrapper();
$compositeException = new CompositeException([
new InvalidEmailException(),
new CompositeException([
new InvalidPasswordException(),
]),
]);
[$emailError, $passwordError] = $unwrapper->unwrap($compositeException);
In this example, errors were retrieved from composite exceptions: $emailError
will be an
instance of InvalidEmailException
and $passwordError
will be an
instance of InvalidPasswordException
that were wrapped in the composite exception.
In symfony application you could use ExceptionUnwrapper service:
public function __construct(
#[Autowire('@phd_exception_toolkit.exception_unwrapper')]
private ExceptionUnwrapper $exceptionUnwrapper,
) {}
This will provide you with full stack of defined unwrappers bundled into a single instance.
If you want to define custom unwrapper, you should decorate
phd_exception_toolkit.exception_unwrapper.stack
service.
If you are using symfony messenger, Symfony\Component\Messenger\Exception\WrappedExceptionsInterface
will be unwrapped automatically.
If you are using Amp, Amp\CompositeException
will be unwrapped automatically.