wunderwerkio/jsonapi-error
最新稳定版本:1.0.0
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:
统计信息
- 总下载量: 4.38k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-03-30