phphd / exception-toolkit
Exception processing toolkit
Installs: 1 183
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 0
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.1
Requires (Dev)
- amphp/amp: ^3.0
- nyholm/symfony-bundle-test: ^3.0
- phphd/coding-standard: ~0.5.3
- phpstan/phpstan: ^1.11
- phpstan/phpstan-phpunit: ^1.4
- phpunit/phpunit: ^10.5
- symfony/config: ^6.0 | ^7.0
- symfony/dependency-injection: ^6.2 | ^7.0
- symfony/http-kernel: ^6.0 | ^7.0
- symfony/messenger: ^6.4 | ^7.0
- symfony/var-dumper: ^6.0 | ^7.0
- tomasvotruba/type-coverage: ^0.3.1
Suggests
- amphp/amp: There's an unwrapper for Amp exceptions of ^3.0 or above
- symfony/messenger: There's an unwrapper for Messenger exceptions of ^6.4 or above
Conflicts
- amphp/amp: >=4.0
- symfony/config: <6.0 || >=8.0
- symfony/dependency-injection: <6.2 || >=8.0
- symfony/http-kernel: <6.0 || >=8.0
- symfony/messenger: >=8.0
README
🧰 Provides a set of tools to handle exceptions in PHP applications.
Installation 📥
-
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],
Provided tools ⚙️
Exception Unwrapper
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.
Symfony integration
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.
Built-in unwrappers
Messenger
If you are using symfony messenger, Symfony\Component\Messenger\Exception\WrappedExceptionsInterface
will be unwrapped automatically.
Amp
If you are using Amp, Amp\CompositeException
will be unwrapped automatically.