studiow / http-exceptions
HTTP exceptions
v1.0.3
2022-08-01 12:37 UTC
Requires
- php: ^7.4 || ^8.0
- ext-json: *
- psr/http-message: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.8
- guzzlehttp/psr7: ^2.4
- phpunit/phpunit: ^9.2
This package is auto-updated.
Last update: 2024-12-29 06:41:03 UTC
README
A helper package for throwing HTTP Exceptions (4xx - 5xx)
Installation
The easiest way to install this package is using composer:
composer require studiow/http-exceptions
Throwing exceptions
The package provides dedicated Exceptions for each of the 4xx and 5xx error status codes. You throw them like you would throw any other exception:
if (! file_exists($my_file)){ throw new \Studiow\Http\Exceptions\ClientError\NotFoundException(); }
Outputting to Json
All of the exceptions have a toJsonResponse method. This method expects a PSR-7 ResponseInterface object and will return a a PSR-7 ResponseInterface object with the appropriate headers, status code and reason phrase set.
$exception = new \Studiow\Http\Exceptions\ClientError\NotFoundException(); /* convert the exception to a ResponseInterfaceObject */ $response = $exception->toJsonResponse(new \GuzzleHttp\Psr7\Response());