Skip to content

SineMah/json-api-error

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

25 Commits
 
 
 
 
 
 
 
 

Repository files navigation

JSON:API error response

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"
        }
    ]
}

About

generic JSON:API error responses

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages