sinema / json-api-error
Format generic JSON:API errors
Installs: 3 158
Dependents: 1
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.2.0|^8.3.0
README
Spec
https://jsonapi.org/examples/#error-objects-basics
Installation
composer require sinema/json-api-error
Usage
Basic Usage
<?php use Sinemah\JsonApi\Error\Error; use Sinemah\JsonApi\Error\ErrorBag; $errors = new ErrorBag(); $errors->add( Error::fromArray( [ 'status' => 404, 'source' => null, 'title' => 'Item not found', 'detail' => sprintf('Item %s not found', 'some-id'), ] ) ); $errors->toArray()
Result as JSON representation
[ { "status": 404, "title": "Item not found", "detail": "Item some-id not found" } ]
Response Usage
<?php use Sinemah\JsonApi\Error\Error; use Sinemah\JsonApi\Error\Response; $response = Response::get(); $response->add( Error::fromArray( [ 'status' => 404, 'source' => null, 'title' => 'Item not found', 'detail' => sprintf('Item %s not found', 'some-id'), ] ) ); $response->toArray()
Result as JSON representation
{ "errors": [ { "status": 404, "title": "Item not found", "detail": "Item some-id not found" } ] }