wunderwerkio/jsonapi-error
Composer 安装命令:
composer require wunderwerkio/jsonapi-error
包简介
Simple class to conveniently create a JSON error that complies with the JSON:API specification.
README 文档
README
This package provides JsonApiError and JsonApiErrorResponse classses to conveniently handle errors following the JSON:API specification.
The JsonApiErrorResponse extends the JsonResponse from symfony/http-foundation, so this package is meant to be used in projects using that.
Table of contents:
Install
Install this package via composer:
composer require wunderwerkio/jsonapi-error
Usage
Return a simple error response
<?php use Symfony\Component\HttpFoundation\Response; use Wunderwerk\JsonApiError\JsonApiErrorResponse; function someRequestHandler(): Response { return JsonApiErrorResponse::fromArray([ 'code' => 'application_error_code', 'title' => 'An error occured!', 'status' => 500, ]); }
The above code would result in a JSON response with the following payload:
{
"errors": [{
"status": 500,
"code": "application_error_code",
"title": "An error occured!"
}]
}
Return multiple errors
<?php use Symfony\Component\HttpFoundation\Response; use Wunderwerk\JsonApiError\JsonApiErrorResponse; function someRequestHandler(): Response { return JsonApiErrorResponse::fromArrayMultiple([ [ 'status' => 422, 'code' => 'validation_failed', 'title' => 'Invalid request payload', 'detail' => 'The "name" field is required.', 'source' => [ 'pointer' => '/data/name' ] ], [ 'status' => 422, 'code' => 'validation_failed', 'title' => 'Invalid request payload', 'detail' => 'The "description" field is required.', 'source' => [ 'pointer' => '/data/description' ] ], ]); }
The above code would result in a JSON response with the following payload:
{
"errors": [{
"status": 422,
"code": "validation_failed",
"title": "Invalid request payload",
"detail": "The \"name\" field is required.",
"source": {
"pointer": "/data/name"
}
}, {
"status": 422,
"code": "validation_failed",
"title": "Invalid request payload",
"detail": "The \"description\" field is required.",
"source": {
"pointer": "/data/description"
}
}]
}
Build response from JsonApiError objects
To ease building a response with multiple errors, the response can also be created by constricting it by
passing an array of JsonApiError objects.
<?php use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Response; use Wunderwerk\JsonApiError\JsonApiError; use Wunderwerk\JsonApiError\JsonApiErrorResponse; function someRequestHandler(Request $request): Response { /** @var JsonApiError[] $errors */ $errors = []; $payload = $request->getContent(); $entity = json_decode($payload, TRUE); // Make sure 'name' field is set. if (!array_key_exists('name', $entity['data'])) { $errors[] = JsonApiError::fromArray([ 'status' => 422, 'code' => 'validation_failed', 'title' => 'Invalid request payload', 'detail' => 'The "name" field is required.', 'source' => [ 'pointer' => '/data/name', ], ]); } // Make sure 'description' field is set. if (!array_key_exists('description', $entity['data'])) { $errors[] = JsonApiError::fromArray([ 'status' => 422, 'code' => 'validation_failed', 'title' => 'Invalid request payload', 'detail' => 'The "description" field is required.', 'source' => [ 'pointer' => '/data/description', ], ]); } if (!empty($errors)) { return new JsonApiErrorResponse($errors); } return new JsonResponse([ 'status' => 'success', ]); }
Local Development
A local dev environment can be created using nix:
# For PHP 8.1 nix develop '#.php81' # For PHP 8.2 nix develop '#.php82' # For PHP 8.3 nix develop '#.php83'
Run tests
composer test
Lint with PHPStan
composer analyze
Credits
This project took inspiration from the following awesome projects:
wunderwerkio/jsonapi-error 适用场景与选型建议
wunderwerkio/jsonapi-error 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.77k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 03 月 30 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 wunderwerkio/jsonapi-error 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wunderwerkio/jsonapi-error 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 7.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-30