Download the PHP package letkode/http-exception-bundle without Composer
On this page you can find all versions of the php package letkode/http-exception-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download letkode/http-exception-bundle
More information about letkode/http-exception-bundle
Files in letkode/http-exception-bundle
Package http-exception-bundle
Short Description HTTP status exceptions and a JSON exception listener for Symfony applications
License MIT
Informations about the package http-exception-bundle
letkode/http-exception-bundle
HTTP status exceptions and a JSON exception listener for Symfony applications.
Installation
Symfony Flex registers the bundle automatically. Otherwise:
Requires a configured Symfony translator and a PSR-3 logger. Install symfony/validator to get per-field errors on 422 responses.
Configuration
All options are optional; defaults are shown.
path_prefix is matched as a plain string prefix, so /api also matches /apiary. An empty string ('') makes the listener handle every path.
Debug traces follow %kernel.debug%; there is nothing to configure.
Using your own exception listener
To handle exceptions with your own listener, turn the bundle's off:
The exceptions, the contracts, TranslationOption and the locale resolver stay available; only the kernel.exception listener is not registered. Your listener can keep relying on HttpStatusExceptionInterface (getStatusCode(), getErrorCode(), getOption()).
Usage
Throw an exception from any service; the listener turns it into JSON.
Every exception has a default errorCode (for example BAD_REQUEST), overridable through the second constructor argument.
Exceptions
| Class | HTTP | Default errorCode |
|---|---|---|
BadRequestException |
400 | BAD_REQUEST |
UnauthorizedException |
401 | UNAUTHORIZED |
ForbiddenException |
403 | FORBIDDEN |
NotFoundException |
404 | NOT_FOUND |
EntityNotFoundException |
404 | ENTITY_NOT_FOUND |
MethodNotAllowedException |
405 | METHOD_NOT_ALLOWED |
NotAcceptableException |
406 | NOT_ACCEPTABLE |
ConflictException |
409 | CONFLICT |
GoneException |
410 | GONE |
PreconditionFailedException |
412 | PRECONDITION_FAILED |
PayloadTooLargeException |
413 | PAYLOAD_TOO_LARGE |
UnsupportedMediaTypeException |
415 | UNSUPPORTED_MEDIA_TYPE |
UnprocessableEntityException |
422 | UNPROCESSABLE_ENTITY |
LockedException |
423 | LOCKED |
PreconditionRequiredException |
428 | PRECONDITION_REQUIRED |
TooManyRequestsException |
429 | TOO_MANY_REQUESTS |
InternalServerErrorException |
500 | INTERNAL_SERVER_ERROR |
NotImplementedException |
501 | NOT_IMPLEMENTED |
BadGatewayException |
502 | BAD_GATEWAY |
ServiceUnavailableException |
503 | SERVICE_UNAVAILABLE |
GatewayTimeoutException |
504 | GATEWAY_TIMEOUT |
Server errors (5xx) are logged as critical.
Translated messages
Pass a TranslationOption to translate the message (which then acts as a translation key):
Without a TranslationOption the message is returned as is.
Extending the exceptions
The exceptions are not final. Create a subclass for a domain case and override defaultErrorCode() to give it its own code:
Anything that catches EntityNotFoundException (or renders it) also handles the subclass. Your own exception class can also extend AbstractHttpStatusException (implement getStatusCode() and defaultErrorCode()) or implement HttpStatusExceptionInterface.
Errors by field
Attach an ErrorsOption to include an errors object in the response, keyed by field. Each message is a string or a Symfony TranslatableInterface, which is translated with the request locale:
Without the option the response has no errors key.
Other exceptions
| Thrown | Response |
|---|---|
UnprocessableEntityHttpException wrapping a ValidationFailedException |
422, errors grouped by field |
Any Symfony HttpExceptionInterface |
its status; framework messages replaced by http.<status> / http.default; traces in debug |
| Anything else | 500 with the http.500 message, details only in the log |
Translations
The bundle ships exceptions.en.yaml and exceptions.es.yaml (keys http.<status>, http.default, validation.failed). Override any key by defining it in your application's translations/exceptions.<locale>.yaml.
Locale
Messages are translated with the locale of the current request. To change that, implement Letkode\HttpExceptionBundle\Contract\LocaleResolverInterface and alias it in your services.yaml:
License
MIT
All versions of http-exception-bundle with dependencies
psr/log Version ^1.1 || ^2.0 || ^3.0
symfony/config Version ^7.0 || ^8.0
symfony/dependency-injection Version ^7.0 || ^8.0
symfony/http-foundation Version ^7.0 || ^8.0
symfony/http-kernel Version ^7.0 || ^8.0
symfony/translation-contracts Version ^3.0
symfony/yaml Version ^7.0 || ^8.0