yuriitatur / exceptions
Simple exception package, to start extending your own classes
Requires
- php: >=8.0
Requires (Dev)
- orchestra/testbench: ^11.3
- phpunit/phpunit: ^12.1.1
- symfony/validator: ^7.3
Suggests
- laravel/framework: To render and report exceptions with the bundled ExceptionServiceProvider
Provides
None
Conflicts
None
Replaces
None
This package is auto-updated.
Last update: 2026-10-26 05:05:25 UTC
README
Exceptions
Composer package for throwing and handling common exceptions.
It features a single base exception class that has to have all your logic and data, you want to report.
Reuse or extend AppException. It has context data, http status code and string error type to cover
most of the use cases that you expect from using an exception.
Why?
Instead of writing multiple error handlers that should be aware of each exception.
Use only one that knows how to render/report/log (or maybe not) your error.
Of course, you will still have to deal with your db/framework/3rd party exceptions, but due to dependency
inversion, you can't make them a part of your core application.
You can also make use of pre-defined InvalidArgumentExeption and ValidationException.
Installation
Composer:
composer require yuriitatur/exceptions
Laravel
The package ships with YuriiTatur\Exceptions\Laravel\ExceptionServiceProvider, registered through package
auto-discovery. It hooks into Laravel's exception handler and:
- renders
AppExceptionas JSON (toArray()payload withgetHttpCode()status) when the request expects JSON; - otherwise renders Laravel's error page with
getHttpCode()status (server errors keep the debug page in debug mode); - skips reporting client errors (http code < 500), the same way Laravel treats its own http and validation exceptions;
- adds
error_typeandhttp_codeto the log context of reported exceptions, next to the exception context.
To change this behaviour, extend the provider, override shouldReport(), reportContext() or render(),
and register your own provider instead (add the package to dont-discover in your app's composer.json).
Testing
composer test
More to come
This is the most basic package of the bunch of cool packages. If you find this work useful, please consider becoming a sponsor.
License
This code is under MIT license, read more in the LICENSE file.