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.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

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

PHP Build Version
Package Version
Requires php Version ^8.4
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
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package letkode/http-exception-bundle contains the following files

Loading the files please wait ...