PHPackages                             lcobucci/error-handling-middleware - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. lcobucci/error-handling-middleware

ActiveLibrary[HTTP &amp; Networking](/categories/http)

lcobucci/error-handling-middleware
==================================

A PSR-15 middleware compatible with RFC 7807

1.4.0(4y ago)6171.3k↓44.1%2[23 PRs](https://github.com/lcobucci/error-handling-middleware/pulls)1MITPHPPHP ^8.0

Since Jun 27Pushed 3y ago1 watchersCompare

[ Source](https://github.com/lcobucci/error-handling-middleware)[ Packagist](https://packagist.org/packages/lcobucci/error-handling-middleware)[ GitHub Sponsors](https://github.com/lcobucci)[ Patreon](https://www.patreon.com/lcobucci)[ RSS](/packages/lcobucci-error-handling-middleware/feed)WikiDiscussions 1.5.x Synced 1mo ago

READMEChangelog (5)Dependencies (16)Versions (33)Used By (1)

Error handling middleware
=========================

[](#error-handling-middleware)

[![Total Downloads](https://camo.githubusercontent.com/4af551ebd8c9c24466cf65ddecf0000199044365bdc477a3abe553ff9724190b/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6c636f62756363692f6572726f722d68616e646c696e672d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lcobucci/error-handling-middleware)[![Latest Stable Version](https://camo.githubusercontent.com/19f7e0c2c4592bec67e1ec76a6ff9ba5418943c3e7a0a1c5467f23c1fbb41f10/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6c636f62756363692f6572726f722d68616e646c696e672d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lcobucci/error-handling-middleware)[![Unstable Version](https://camo.githubusercontent.com/780f057704d2399771c6ed0518c05e009678637d7e506707ffbd36cc2cfd4d8f/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f767072652f6c636f62756363692f6572726f722d68616e646c696e672d6d6964646c65776172652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/lcobucci/error-handling-middleware)

[![Build Status](https://camo.githubusercontent.com/b1235a1ddb2d32361cae22bf984d1e29a47ab48546c57e6ed64b897bd9451e07/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f776f726b666c6f772f7374617475732f6c636f62756363692f6572726f722d68616e646c696e672d6d6964646c65776172652f504850556e697425323074657374732f312e332e783f7374796c653d666c61742d737175617265)](https://github.com/lcobucci/error-handling-middleware/actions?query=workflow%3A%22PHPUnit%20Tests%22+branch%3A1.3.x)[![Code Coverage](https://camo.githubusercontent.com/a2451369c1e15ce0e778ace1ac4695d407d4f5fd2e5b16e596d8814c52dd17af/68747470733a2f2f636f6465636f762e696f2f67682f6c636f62756363692f6572726f722d68616e646c696e672d6d6964646c65776172652f6272616e63682f312e332e782f67726170682f62616467652e737667)](https://codecov.io/gh/lcobucci/error-handling-middleware)

Motivation
----------

[](#motivation)

There are many PHP implementations for the [RFC 7807](https://tools.ietf.org/html/rfc7807), even providing [PSR-15 middleware](https://www.php-fig.org/psr/psr-15/). 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
------------

[](#installation)

This package is available on [Packagist](http://packagist.org/packages/lcobucci/error-handling-middleware), and we recommend you to install it using [Composer](http://getcomposer.org):

```
composer require lcobucci/error-handling-middleware
```

Usage
-----

[](#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

[](#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](https://github.com/lcobucci/content-negotiation-middleware) one:

```
