jenky/api-error 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

jenky/api-error

Composer 安装命令:

composer require jenky/api-error

包简介

Standardize error responses in API applications

README 文档

README

Latest Version on Packagist Github Actions Codecov Total Downloads Software License

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

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

Symfony HTTP Foundation Based Framework

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

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.

Internally, the error formatter transforms the exception into a Problem, 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.

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. Alternatively, you can extend the AbstractErrorFormatter, provided for the sake of convenience, and define your own error format in the getFormat method.

Exception Transformations

An exception transformer is used to customize the transformation of a Throwable exception into a Problem, 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 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 to run all of them.

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.

jenky/api-error 适用场景与选型建议

jenky/api-error 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 531 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 02 月 28 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「api-problem」 「rfc7808」 「api-error」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 jenky/api-error 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jenky/api-error 我们能提供哪些服务?
定制开发 / 二次开发

基于 jenky/api-error 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-02-28