Download the PHP package lcobucci/error-handling-middleware without Composer

On this page you can find all versions of the php package lcobucci/error-handling-middleware. 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 error-handling-middleware

Error handling middleware

Total Downloads Latest Stable Version Unstable Version

Build Status Code Coverage

Motivation

There are many PHP implementations for the RFC 7807, even providing PSR-15 middleware. However, most of them - if not all - mix content negotiation, logging, and formatting with error handling. Some even force you to throw specific types of exceptions in order for them to work.

I believe that those aren't the best design decisions and that we need more flexibility to solve this problem.

Installation

This package is available on Packagist, and we recommend you to install it using Composer:

Usage

In order to us this package you must add the middleware to your pipeline, configuring the desired behaviour (debug info strategy and status code extraction strategy).

Once this is set you'll be able to have your errors/exceptions converted into the correct HTTP responses.

Middleware position

This package provides two middleware for handling errors: error logging and error conversion.

They are designed to be used in the very beginning of the HTTP middleware pipeline, just after the content negotiation one:

With that we'll be able to perform the logging and conversion in the correct order, delegating the content negotiation and formatting to ContentTypeMiddleware - using the configured formatters.

Important

The ErrorConversionMiddleware uses an UnformattedResponse to let the ContentTypeMiddleware perform the formatting. Make sure you have configured formatters for the MIME types application/problem+json and/or application/problem+xml.

It also makes the error/exception available in the error attribute of the response, so you may access it (if needed) by using another middleware between ErrorConversionMiddleware and ContentTypeMiddleware.

Configuring the conversion middleware behaviour

There're two extension points that you can use for that: debug info strategy and status code extraction strategy.

You can also configure the response body attributes by implementing certain interfaces in your exceptions.

Debug info strategy

This defines how the _debug property should be generated in the response body. We provide two default implementations - one designed for production mode and the other for development mode.

To configure this you must pass the desired implementation (or a customised one) as the second argument of the ErrorConversionMiddleware constructor.

To provide your own implementation you need to create a class that implements the DebugInfoStrategy interface.

Status code extraction strategy

This defines how the translation from error/exception to HTTP status code should be done. We provide a single default implementation for that, which is based on class maps.

To configure this you must pass the desired implementation (or a customised one) as the third argument of the ErrorConversionMiddleware constructor.

To provide your own implementation you need to create a class that implements the StatusCodeExtractionStrategy interface.

Default class map

The default map uses the marker interfaces in this packages to perform such translation. If the error/exception doesn't implement any of the marker interfaces, the error/exception code will be used (when it's different than zero), or fallback to the status code 500 (Internal Server Error).

The default map is:

This allows us to create our own exceptions that are automatically converted to the correct status code:

Important: you SHOULD NOT implement more than one of the marker interfaces, otherwise you may have unexpected results.

Customising the response body properties

With this library, you may modify the type and title properties of the generated response and also append new members to it.

That's done by implementing the Typed, Titled, and/or Detailed interfaces - you don't necessarily need to implement all of them, only the ones you want.

The example below shows how to represent one of the samples in the RFC 7807:

License

MIT, see LICENSE.


All versions of error-handling-middleware with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-json Version *
fig/http-message-util Version ^1.1
lcobucci/content-negotiation-middleware Version ^3.1
psr/http-factory Version ^1.0
psr/http-message Version ^1.0
psr/http-server-handler Version ^1.0
psr/http-server-middleware Version ^1.0
psr/log Version ^3.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 lcobucci/error-handling-middleware contains the following files

Loading the files please wait ....