jenky/api-error-bundle

A bundle that formats the JSON api problem

0.1.0 2025-03-04 15:29 UTC

This package is auto-updated.

Last update: 2025-03-05 03:42:47 UTC


README

Latest Version on Packagist Github Actions Codecov Total Downloads Software License

Standardize error responses in your Symfony application using RFC7807 Problem details or any custom error format.

Installation

You can install the package via composer:

composer require jenky/api-error-bundle-bunder

If you are not using symfony/flex, you'll have to manually add the bundle to your bundles file:

// config/bundles.php

return [
    // ...
    Jenky\Bundle\ApiError\ApiErrorBundle::class => ['all' => true],
];

Configuration

Generic Error Response Format

By default all thrown exceptions will be transformed into the following format:

{
    '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
}

The debug information only available when application debug mode (kernel.debug) is on.

Example:

curl --location --request GET 'http://myapp.test/api/not-found' \
--header 'Accept: application/json'
{
  "message": "Not Found",
  "status": 404,
  "code": 0,
}

RFC7807 Problem details

You will need to create an alias from Jenky\ApiError\Formatter\ErrorFormatter interface to api_error.error_formatter.rfc7807.

# config/services.yaml

services:
    # ...
    Jenky\ApiError\Formatter\ErrorFormatter: '@api_error.error_formatter.rfc7807'

For more information, please visit https://symfony.com/doc/current/service_container/autowiring.html#dealing-with-multiple-implementations-of-the-same-type.

Custom Error Format

Create your own custom formatter that implements ErrorFormatter. Alternatively, you can extend the AbstractErrorFormatter, provided for the sake of convenience, and define your own error format in the getFormat method.

Register your service if needed, in case autowire and autoconfigure are disabled. Then create the alias:

# config/services.yaml

services:
    # ...
    api_error.error_formatter.custom:
        class: MyCustomErrorFormatter
        #

    Jenky\ApiError\Formatter\ErrorFormatter: '@api_error.error_formatter.custom'

Alternatively, you can use the GenericErrorFormatter and configure ErrorFormatter service with a Configurator:

# services.yaml

services:
    #

    Jenky\ApiError\Formatter\ErrorFormatter:
        parent: Jenky\ApiError\Formatter\AbstractErrorFormatter
        class: Jenky\ApiError\Formatter\GenericErrorFormatter
        configurator: '@App\ApiError\ErrorFormatterConfigurator'
// App\ApiError\ErrorFormatterConfigurator.php

use Jenky\ApiError\Formatter\GenericErrorFormatter;

final class ErrorFormatterConfigurator
{
    public function __invoke(GenericErrorFormatter $formatter): void
    {
        $formatter->setFormat([
            'message' => '{title}',
            'status' => '{status_code}',
            'code' => '{code}',
            'errors' => '{errors}',
        ]);
    }
}

Exception Transformations

If you want to add custom transformations, you should create a new class that implements the ExceptionTransformer. With autoconfigured enabled, you're all set. Otherwise, register it in Symfony container with the api_error.exception_transformer tag.

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Contributing

Please see CONTRIBUTING and CODE_OF_CONDUCT for details.

Security

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

Credits

License

The MIT License (MIT). Please see License File for more information.