tembra/jsonapi-errors-php

A PHP implementation of JSON API Errors compliant format.

v0.8.0 2016-06-30 18:35 UTC

This package is not auto-updated.

Last update: 2024-11-09 20:44:58 UTC


README

License Version Total Installs StyleCI

Description

JSON API logo

This framework agnostic package implements a simple and efficient way to throw or respond errors in JSON API specification version v1.0 as described in JSON API Errors. It also helps creating documentation for these errors on your application. It is based on 3rd party package neomerx/json-api that fully implements the JSON API Format.

It greatly simplifies the error processing with high code quality.

Still framework agnostic, you can easily integrate it with Laravel/Lumen and Dingo API.

Milestone to v1.0

  • Makes JSON API Errors as simple as calling a function
  • Standardize the errors
  • Provide most common error functions for HTTP Status Codes
  • Throw an exception or return the JSON string
  • Override JSON API Error Objects members
  • Support for Localization
  • Generate Documentation for Application Error Codes
  • Build PHPUnit tests

Sample usage

Assuming you don't want a specific class to standardize the errors and also don't want the documentation or localization, you can use as simple as this:

echo MyJsonApiErrors::badRequest([
  827 => [
    'title' => 'Another Error',
    'detail' => 'Detailed error description'
  ]
], false);

will output as string

{
  "errors": [
    {
      "status": "400",
      "code": "827",
      "title": "Another Error",
      "detail": "Detailed error description"
    }
  ]
}

The first parameter is an associative array where key is the error code in JSON API compliant format and value is another associative array where key/value pairs are some others members that JSON API Error Objects may have.

The second parameter false is to define whether a JsonApiException should be thrown or only the JSON string should be returned.

For more advanced usage please check out the Wiki.

Questions?

Do not hesitate to contact me on tdt@mytdt.com.br or post an issue.

License

BSD 3-Clause. Please see License File for more information.