PHPackages                             jenky/api-error - 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. jenky/api-error

ActiveLibrary

jenky/api-error
===============

Standardize error responses in API applications

0.3.1(5mo ago)05162MITPHPPHP ^8.1CI passing

Since Mar 4Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/jenky/api-error)[ Packagist](https://packagist.org/packages/jenky/api-error)[ Docs](https://github.com/jenky/api-error)[ RSS](/packages/jenky-api-error/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (5)Versions (5)Used By (2)

Api Error
=========

[](#api-error)

[![Latest Version on Packagist](https://camo.githubusercontent.com/01921bc33ff82032a3b4129f0672e49a7434e3c62f2eee3f217fb8faa36c8225/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a656e6b792f6170692d6572726f722e7376673f6c6f676f3d7061636b6167697374267374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/jenky/api-error)[![Github Actions](https://camo.githubusercontent.com/0bbda88bf7db8b8d0c01ae54f8a69ad268aa212ad079abf43cdf0754a425b6d5/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f6a656e6b792f6170692d6572726f722f74657374696e672e796d6c3f6272616e63683d6d61696e266c6162656c3d616374696f6e73266c6f676f3d676974687562267374796c653d666f722d7468652d6261646765)](https://github.com/jenky/api-error)[![Codecov](https://camo.githubusercontent.com/d25198f507fa35005c87667eaf92dc8e6fd1bc2e494cf2d829417855b4d35149/68747470733a2f2f696d672e736869656c64732e696f2f636f6465636f762f632f6769746875622f6a656e6b792f6170692d6572726f723f6c6f676f3d636f6465636f76267374796c653d666f722d7468652d6261646765)](https://codecov.io/gh/jenky/api-error)[![Total Downloads](https://camo.githubusercontent.com/89e012419a22fa086a1ece7643ff79fbeb3678da4544fccd9e64bfeaadf15c28/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a656e6b792f6170692d6572726f722e7376673f7374796c653d666f722d7468652d6261646765)](https://packagist.org/packages/jenky/api-error)[![Software License](https://camo.githubusercontent.com/9897f4467850972a38c7db9a4d38280b8fcdac0ada00e9c8c0a72ecfa8551653/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666f722d7468652d6261646765)](LICENSE.md)

This package provides an implementation for API error formatting. It can be integrated throughout your code and should result in a standardized error response format for HTTP APIs.

Quick start
-----------

[](#quick-start)

Since handling the exceptions is up to the framework, here are a list of supported integrations:

### Symfony HTTP Foundation Based Framework

[](#symfony-http-foundation-based-framework)

- [Symfony](https://github.com/jenky/api-error-bundle)
- [Laravel](https://github.com/jenky/hades)

### Bring Your Own

[](#bring-your-own)

You can install the package via composer:

```
composer require jenky/api-error
```

The usage may vary depending on your project. Typically, you should handle it in your global exception handler. Here is a minimal example:

```
use Jenky\ApiError\Formatter\GenericErrorFormatter;
use Jenky\ApiError\Transformer\ChainTransformer;

$transformer = new ChainTransformer([
    // ... list of transformers
])
$formatter = new GenericErrorFormatter(true, $transformer);

// or simply without transformer and debug is off
$formatter = new GenericErrorFormatter();

/** @var \Throwable $exception */
return \json_encode($formatter->format($exception));
```

Building Blocks
---------------

[](#building-blocks)

### Error Formatter

[](#error-formatter)

The error formatter is the main entry point of the package. It formats the `Throwable` exception into a serializable version, allowing the data structure to be used as the response body, typically in `JSON`. An error formatter must implement [`ErrorFormatter`](https://github.com/jenky/api-error/blob/main/src/Formatter/ErrorFormatter.php).

Internally, the error formatter transforms the exception into a [`Problem`](https://github.com/jenky/api-error/blob/main/src/Problem.php), which should return an array as context data for the given exception. This array can contain anything, and when combined with the predefined response error format, will be used to replace placeholders with contextual data.

You can always create your own `Problem` using the [Exception Transformer](#exception-transformations).

`GenericErrorFormatter`

```
{
    'message' => '{message}', // The exception message
    'status' => '{status_code}', // The corresponding HTTP status code, defaults to 500
    'code' => '{code}' // The exception int code
    'debug' => '{debug}', // The debug information
}
```

`Rfc7807ErrorFormatter`

```
{
    'type' => '{type}',
    'title' => '{status_text}', // The corresponding HTTP status text
    'detail' => '{message}',
    'status' => '{status_code}', // The corresponding HTTP status code, defaults to 500
    'invalid-params' => '{invalid_params}',
    'debug' => '{debug}', // The debug information
}
```

- Placeholder names MUST correspond to keys in the context array.
- Placeholder names MUST be delimited with a single opening brace `{` and a single closing brace `}`. There MUST NOT be any whitespace between the delimiters and the placeholder name.
- Placeholder names SHOULD be composed only of the characters `A-Z`, `a-z`, `0-9`, underscore `_`, and period `.`.

**Custom Error Format**

Create your own custom formatter that implements [`ErrorFormatter`](https://github.com/jenky/api-error/blob/main/src/Formatter/ErrorFormatter.php). Alternatively, you can extend the [`AbstractErrorFormatter`](https://github.com/jenky/api-error/blob/main/src/Formatter/AbstractErrorFormatter.php), provided for the sake of convenience, and define your own error format in the `getFormat` method.

### Exception Transformations

[](#exception-transformations)

An exception transformer is used to customize the transformation of a `Throwable` exception into a [`Problem`](https://github.com/jenky/api-error/blob/main/src/Problem.php), allowing you to add or modify the context data. If you want to add custom transformations, you should create a new class that implements the [`ExceptionTransformer`](https://github.com/jenky/api-error/blob/main/src/Transformer/ExceptionTransformer.php) and pass it as a second argument of the error formatter.

```
new GenericFormatter(transformer: new MyExceptionTransformer);
// or
new Rfc7807ErrorFormatter(transformer: new MyExceptionTransformer);
```

If you have multiple transformers, use the [`ChainTransformer`](https://github.com/jenky/api-error/blob/main/src/Transformer/ChainTransformer.php) to run all of them.

Testing
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CODE\_OF\_CONDUCT](CODE_OF_CONDUCT.md) for details.

Security
--------

[](#security)

If you discover any security related issues, please email  instead of using the issue tracker.

Credits
-------

[](#credits)

- [Lynh](https://github.com/jenky)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

36

—

LowBetter than 82% of packages

Maintenance73

Regular maintenance activity

Popularity14

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity39

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~95 days

Total

4

Last Release

153d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/783e915bb411d566e8f1035f197842db5e870a2a995b3943bcbe5db2f9abf09b?d=identicon)[Milano](/maintainers/Milano)

---

Top Contributors

[![jenky](https://avatars.githubusercontent.com/u/1808758?v=4)](https://github.com/jenky "jenky (28 commits)")

---

Tags

api-errorapi-error-handlerapi-problemrfc7807rfc7808api-problemapi-error

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/jenky-api-error/health.svg)

```
[![Health](https://phpackages.com/badges/jenky-api-error/health.svg)](https://phpackages.com/packages/jenky-api-error)
```

###  Alternatives

[laravel/framework

The Laravel Framework.

34.7k509.9M17.0k](/packages/laravel-framework)[symfony/http-kernel

Provides a structured process for converting a Request into a Response

8.1k822.4M6.8k](/packages/symfony-http-kernel)[symfony/framework-bundle

Provides a tight integration between Symfony components and the Symfony full-stack framework

3.6k235.4M9.7k](/packages/symfony-framework-bundle)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.1k84.2M225](/packages/laravel-horizon)[sulu/sulu

Core framework that implements the functionality of the Sulu content management system

1.3k1.3M152](/packages/sulu-sulu)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
