jenky/hades 问题修复 & 功能扩展

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

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

jenky/hades

Composer 安装命令:

composer require jenky/hades

包简介

Error response formatter for Laravel app

README 文档

README

Latest Version on Packagist Github Actions Codecov Total Downloads Software License

Dealing with errors when building an API can be a pain. Instead of manually building error responses you can simply throw an exception and the Hades will handle the response for you.

Installation

You may use Composer to install this package into your Laravel project:

$ composer require jenky/hades

Configuration

Generic Error Response Format

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

{
    'message' => ':message', // The exception message
    'status' => ':status_code', // The corresponding HTTP status code, default to 500
    'errors' => ':errors', // The error bag, typically validation error messages
    'code' => ':code', // The exception code
    'debug' => ':debug', // The debug information
}

The debug information only available when application is not in production environment and debug mode is on.

Example:

curl --location --request GET 'http://myapp.test/api/user' \
--header 'Accept: application/json'
{
  "message": "Unauthenticated.",
  "type": "AuthenticationException",
  "status": 401,
  "code": 0,
}

Any keys that aren't replaced with corresponding values will be removed from the final response.

Formatting Exception

If you would like to use different error format for your application, you should call the Hades::errorFormat() method in the boot method of your App\Providers\AppServiceProvider class:

use Jenky\Hades\Hades;

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    Hades::errorFormat([
        'message' => ':message',
        'error_description' => ':error',
    ]);
}

Customizing Exception Response

Hades uses api-error internally. Please see the exception transformations to see how it works.

To add a custom transformers, you can use the config file

// configs/hades.php

return [

    'transformers' => [
        App\Exceptions\Transformers\MyCustomTransformer::class,
    ],

];

Alternatively, if Laravel can't inject your custom exception transformer, you may wish to register the exception transformer with the api_error.exception_transformer tag in your service provider. Typically, you should call this method from the register method of your application's App\Providers\AppServiceProvider class:

use App\Exceptions\Transformers\MyCustomTransformer;
use Illuminate\Contracts\Debug\ExceptionHandler;

public function register(): void
{
    $this->app->bind(MyCustomTransformer::class, static fn () => 'your binding logic');

    $this->app->tag('api_error.exception_transformer', MyCustomTransformer::class);
}

Content Negotiation

Forcing the JSON Response

By default, Laravel expects the request should contains header Accept with the MIME type application/json or custom MIME with json format such as application/vnd.myapp.v1+json in order to return JSON response. Otherwise your may get redirected to login page if the credentials are invalid or missing/passing invalid authorization token.

While this is a good design practice, sometimes you may wish to attach the header to request automatically, such as using Laravel as pure API backend. To do this, you should call the Hades::forceJsonOutput() method within the boot method of your App\Providers\AppServiceProvider.

use Jenky\Hades\Hades;

public function boot(): void
{
    Hades::forceJsonOutput();
}

Hades will add the header Accept: application/json to all incoming API requests. If you want to use custom MIME type, you may use the withMimeType to specify the MIME type:

Hades::forceJsonOutput()
    ->withMimeType('application/vnd.myapp.v1+json');

Identify API Requests

In order to force the response to return JSON output, Hades needs to identify the incoming request so it doesn't add the Accept header on your normal HTML pages.

By default, all your API routes defined in routes/api.php have /api URI prefix automatically applied. Hades will inspects the incoming request URI and determines it's URI matches the /api prefix.

To customize this behavior, you may pass the closure to Hades::forceJsonOutput() to instruct Hades how to identify the incoming request:

use Illuminate\Http\Request;

Hades::forceJsonOutput(static function (Request $request) {
    return $request->is('api/v1/*');
});

If your application is built solely for API use, you can configure the request to always return a JSON response.

use Illuminate\Http\Request;

Hades::forceJsonOutput(static fn () => true);

Change log

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

Testing

$ composer test

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/hades 适用场景与选型建议

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

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

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-02-22