New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

nextdots/api-exceptions

JSON exceptions for your API
385
Install
composer require nextdots/api-exceptions:dev-master
PHP:^5.3.3 || ^7.0
License:MIT
Last Updated:Jan 19, 2017
Links: GitHub  ·  Packagist
Maintainer: whoan

API Exceptions

Convert your Lumen exceptions into JSON:

{
  "code": nnn,
  "message": "a message",
  "description": "a description",
  "errors": [
    "error 1",
    "error 2",
    ...
  ]
}

Installation

Set your composer.json to allow less stable packages:

"minimum-stability" : "dev",  
"prefer-stable" : true

Require the package as usual:

composer require nextdots/api-exceptions

Change app/Exceptions/Handler.php:

  • Change this:

    use Laravel\Lumen\Exceptions\Handler as ExceptionHandler;
    
  • To this:

    use ApiExceptions\Handler as ExceptionHandler;
    

On Linux, you can do the same as above with the following command:

sed -i 's/Laravel\\Lumen\\Exceptions\\Handler/ApiExceptions\\Handler/' app/Exceptions/Handler.php

Example

use ApiExceptions\JsonResponseException;

$app->get('/throw-exception', function () {
    throw new JsonResponseException(400, "an exception", "a description");
});

Related Packages

bwt-team/laravel-api

Package helping with API development

11,339 1
noitran/lumen-form-requests

Missing form requests functionality for lumen, ported from laravel framework.

4,750 5
nebo15/lumen.rest

Components for fast building REST API for Lumen

3,761 1
ngabor84/lumen-api-query-parser

A query parser for REST-APIs based on laravel's lumen framework

1,485 3
soufiene-slimi/lumen-form-requests

Missing form requests functionality for lumen, ported from laravel framework.

100 0