Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/wunderwerkio/jsonapi-error

Simple class to conveniently create a JSON error that complies with the JSON:API specification.
https://github.com/wunderwerkio/jsonapi-error

drupal json-api php-library php8 symfony

Last synced: 8 days ago
JSON representation

Simple class to conveniently create a JSON error that complies with the JSON:API specification.

Awesome Lists containing this project

README

        

# JSON:API Error
[![Test](https://github.com/wunderwerkio/jsonapi-error/actions/workflows/main.yml/badge.svg)](https://github.com/wunderwerkio/jsonapi-error/actions/workflows/main.yml)

This package provides `JsonApiError` and `JsonApiErrorResponse` classses to conveniently handle errors following the [JSON:API specification](https://jsonapi.org/format/#errors).

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)
- [Usage](#usage)
- [Return a simple error response](#return-a-simple-error-response)
- [Return multiple errors](#return-multiple-errors)
- [Build response from `JsonApiError` objects](#build-response-from-jsonapierror-objects)
- [Local Development](#local-development)
- [Credits](#credits)

## Install

Install this package via composer:

```bash
composer require wunderwerkio/jsonapi-error
```

## Usage

### Return a simple error response

```php
'application_error_code',
'title' => 'An error occured!',
'status' => 500,
]);
}
```

The above code would result in a JSON response with the following payload:

```json
{
"errors": [{
"status": 500,
"code": "application_error_code",
"title": "An error occured!"
}]
}
```

### Return multiple errors

```php
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:

```json
{
"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
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`:

```bash
# For PHP 8.1
nix develop '#.php81'
# For PHP 8.2
nix develop '#.php82'
# For PHP 8.3
nix develop '#.php83'
```

**Run tests**

```bash
composer test
```

**Lint with PHPStan**

```bash
composer analyze
```

## Credits

This project took inspiration from the following awesome projects:

- [`SineMah/json-api-error`](https://github.com/SineMah/json-api-error)