定制 jenky/api-error-bundle 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jenky/api-error-bundle

Composer 安装命令:

composer require jenky/api-error-bundle

包简介

A bundle that formats the JSON api problem

README 文档

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.

统计信息

  • 总下载量: 93
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-03-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固