2017 © Pedro Peláez
 

library jer

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

image

lalu/jer

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

  • Friday, June 3, 2016
  • by adamnguyenit
  • Repository
  • 1 Watchers
  • 1 Stars
  • 40 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Json Exception Response

Latest Stable Version Build Status Coverage Status Total Downloads License, (*1)

A Laravel/Lumen package for structing API exception response in JSON followed http://jsonapi.org/., (*2)

Install

Via Composer, (*3)

$ composer require lalu/jer

Laravel

Once this has finished, you will need to add the service provider to the providers array in your config/app.php as follows:, (*4)

'providers' => [
    // ...
    LaLu\JER\JERServiceProvider::class,
]

If you want to use alias, also in the app.php config file, under the aliases array, you may want to add facades as follows:, (*5)

'aliases' => [
    // ...
    'JER' => LaLu\JER\Facades\JERFacade::class
]

Then, publish the localization by running:, (*6)

php artisan vendor:publish

Lumen

Open bootstrap/app.php and add this line:, (*7)

$app->register(LaLu\JER\JERServiceProvider::class);

If you want to use alias, also in your bootstrap/app.php, make sure you have uncommented, (*8)

$app->withFacades();

Then, add this line:, (*9)

class_alias(LaLu\JER\Facades\JERFacade::class, 'JER');

For localization, you have to create messages.php under resources/lang/vendor/lalu-jer/en (default is en - English). All built-in message ids are in here, (*10)

Usage

In the app\Exceptions\Handler.php, let the class extends LaLu\JER\ExceptionHandler., (*11)

use LaLu\JER\ExceptionHandler;

class Handler extends ExceptionHandler
{
    // ...
}

Then all of Exceptions will handle by this package., (*12)

You can also use abort or throw new \Exception\You\Want() to raise and response exception., (*13)

Advanced

Add meta to response json? It's not a big deal., (*14)

use LaLu\JER\ExceptionHandler;

class Handler extends ExceptionHandler
{
    public $meta = [
        'meta_field_1' => 'meta_value_1',
        // ...
    ];

    // ...
}

Or, (*15)

use LaLu\JER\ExceptionHandler;

class Handler extends ExceptionHandler
{
    public function beforeRender($request, Exception $exception)
    {
        $this->meta = [
            'meta_field_1' => 'meta_value_1',
            // ...
        ];
    }

    // ...
}

With beforeRender which will be raised before the render method, you can do more logics to set meta, headers and so on., (*16)

If you want to custom the response of some Exception classes, just override the getExceptionError., (*17)

use LaLu\JER\ExceptionHandler;
use LaLu\JER\Error;

class Handler extends ExceptionHandler
{
    // ...

    /**
     * Get exception jsonapi data.
     *
     * @param \Exception $exception
     *
     * @return array
     */
    protected function getExceptionError(Exception $exception)
    {
        if ($exception instanceof \Exception\You\Want) {
            // status must be an integer and is a HTTP error status code
            $status = 400;
            // headers must be an array of key value
            $headers = [];
            $content = [
                'title' => 'Your exception custom title',
                'detail' => 'Your exception custom detail',
            ];
            // error can be an instance/array items of \LaLu\JER\Error or array of error array
            $error = new Error(['version' => $this->jsonapiVersion], $content);
            $error->status = '400';
            // ...
            return [$status, $error, $headers];
        } elseif ($exception instanceof \Other\Exception) {
            return [400, [['title' => 'Your request is bad request']], []];
        } else {
            return parent::getExceptionError($exception);
        }
    }
}

If you want to custom error json response, feel free to use this function:, (*18)

$option = [
    'version' => '1.0', // JSONAPI specification version
    'status' => 400, // HTTP status code
    'headers' => ['My-Custom-Header' => 'Value'], // Response headers,
    'exception' => new \Exception(), // Exception
];
$attributes = [
    'meta' => [
        'apiVersion' => '1.0.0',
        'author' => 'thanh-taro',
    ],
    'errors' => new Error(['version' => '1.0'], ['title' => 'My custom error', 'detail' => 'This is an error response']), // Error content
];
$response = \JER::getResponse($option, $attributes);

Note that JER is an alias, if you didn't config for alias, you may use, (*19)

(new \LaLu\JER\JsonExceptionResponse())->getResponse($option, $attributes);

License

The MIT License (MIT)., (*20)

The Versions

03/06 2016

dev-master

9999999-dev

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

by Nguyen Tri Thanh

02/06 2016

v1.0.1

1.0.1.0

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

by Nguyen Tri Thanh

02/06 2016

v1.0.1.x-dev

1.0.1.9999999-dev

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

by Nguyen Tri Thanh

02/06 2016

v1.0.0.x-dev

1.0.0.9999999-dev

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

by Nguyen Tri Thanh

31/05 2016

v1.0.0

1.0.0.0

A laravel/lumen package for structing API exception response in JSON followed http://jsonapi.org/

  Sources   Download

MIT

The Requires

  • php ~5.5|~7.0

 

The Development Requires

by Nguyen Tri Thanh