3sidedcube/laravel-api-errors
Composer 安装命令:
composer require 3sidedcube/laravel-api-errors
包简介
A lightweight package for handling API error responses.
README 文档
README
This package provides an easy way to manage and handle error response for JSON API's.
Installation
You can install the package via composer:
composer require 3sidedcube/laravel-api-errors
Usage
There are 2 ways of generating an API error response:
API Error Exception
This package provides an exception called ApiErrorException which you can extend. There are 3 methods which can
be set (2 of which are required):
code()- This is a short string indicating the error code (required).message()- A human-readable message providing more details about the error (required).statusCode()- This HTTP status code of the error response. By default, this is set to 400 and is optional.
Once you have an exception, you can use the fromException() method to generate an API error response:
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse; use ThreeSidedCube\LaravelApiErrors\Exceptions\ApiErrorException; class UserBannedException extends ApiErrorException { /** * A short error code describing the error. * * @return string */ public function code(): string { return 'user_account_banned'; } /** * A human-readable message providing more details about the error. * * @return string */ public function message(): string { return 'User account banned.'; } /** * The api error status code. * * @return int */ public function statusCode(): int { return 403; } } $exception = new UserBannedException(); // This will return an instance of JsonResponse $response = ApiErrorResponse::fromException($exception);
Returning this response would generate the following json response:
{
"error": {
"code": "user_account_banned",
"message": "User account banned."
}
}
Automatically returning the exception response
If you want to automatically return the JSON response from the exception, you can add the exception to the $dontReport
array in your app/Exceptions/Handler.php like so:
use ThreeSidedCube\LaravelApiErrors\Exceptions\ApiErrorException; protected $dontReport = [ ApiErrorException::class, ];
Passing data directly
Alternatively you can use the create() method to create an API error response:
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse; // This will return an instance of JsonResponse $response = ApiErrorResponse::create('user_account_banned', 'User account banned.', 403);
Returning this response would generate the following json response:
{
"error": {
"code": "user_account_banned",
"message": "User account banned."
}
}
Additional data
If you would like to pass additional "meta" data to the response, you can use the meta() method or pass an array to
the create method like so:
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse; use ThreeSidedCube\LaravelApiErrors\Exceptions\ApiErrorException; class UserBannedException extends ApiErrorException { /** * A short error code describing the error. * * @return string */ public function code(): string { return 'user_account_banned'; } /** * A human-readable message providing more details about the error. * * @return string */ public function message(): string { return 'User account banned.'; } /** * The api error status code. * * @return int */ public function statusCode(): int { return 403; } /** * Any additional metadata to be included in the response. * * @return array */ public function meta(): array { return [ 'foo' => 'bar', ]; } } $exception = new UserBannedException(); // This will return an instance of JsonResponse $response = ApiErrorResponse::fromException($exception);
or
use ThreeSidedCube\LaravelApiErrors\ApiErrorResponse; // This will return an instance of JsonResponse $response = ApiErrorResponse::create('user_account_banned', 'User account banned.', 403, ['foo' => 'bar']);
Returning this response would generate the following json response:
{
"error": {
"code": "user_account_banned",
"message": "User account banned.",
"meta": {
"foo": "bar"
}
}
}
Testing
composer test
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
License
Laravel API Errors is open-sourced software licensed under the MIT license.
3sidedcube/laravel-api-errors 适用场景与选型建议
3sidedcube/laravel-api-errors 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 15.49k 次下载、GitHub Stars 达 4, 最近一次更新时间为 2021 年 12 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「3sidedcube」 「laravel-api-errors」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 3sidedcube/laravel-api-errors 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 3sidedcube/laravel-api-errors 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 3sidedcube/laravel-api-errors 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
统计信息
- 总下载量: 15.49k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 4
- 点击次数: 11
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-12-23