PHPackages                             salmanzafar/laravel-api-exceptions-handler - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. salmanzafar/laravel-api-exceptions-handler

AbandonedLibrary[API Development](/categories/api)

salmanzafar/laravel-api-exceptions-handler
==========================================

A Laravel package that makes it easy to handle and customize api exceptions and responses

v1.0.4(5y ago)6339↓100%MITPHPPHP &gt;=7.3.0

Since Jan 30Pushed 3y ago1 watchersCompare

[ Source](https://github.com/salmanzafar949/laravel-api-exceptions-handler)[ Packagist](https://packagist.org/packages/salmanzafar/laravel-api-exceptions-handler)[ Fund](https://www.buymeacoffee.com/salmanzafar949)[ Patreon](https://www.patreon.com/salmanzafar949)[ RSS](/packages/salmanzafar-laravel-api-exceptions-handler/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (1)Versions (6)Used By (0)

laravel-api-exceptions-handler
==============================

[](#laravel-api-exceptions-handler)

[![stars](https://camo.githubusercontent.com/242df6730b6e52cff34541f12bfeefa3e5dd3f711f7e60ec821791c225eaca7a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)](https://camo.githubusercontent.com/242df6730b6e52cff34541f12bfeefa3e5dd3f711f7e60ec821791c225eaca7a/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)[![issues](https://camo.githubusercontent.com/940bd4a13e4661169e371a92b361e7021aa7b49ccb1932ea5938164a63e713b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)](https://camo.githubusercontent.com/940bd4a13e4661169e371a92b361e7021aa7b49ccb1932ea5938164a63e713b6/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)[![forks](https://camo.githubusercontent.com/62b55724a78ce96fd66fec4a7dc848db4e22947911779abc9756102b1f4fc93c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)](https://camo.githubusercontent.com/62b55724a78ce96fd66fec4a7dc848db4e22947911779abc9756102b1f4fc93c/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)[![license](https://camo.githubusercontent.com/4bb134afdf8bafef5f959665d72972932aae995dae3300a83b053d5f07ca47cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)](https://camo.githubusercontent.com/4bb134afdf8bafef5f959665d72972932aae995dae3300a83b053d5f07ca47cd/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f73616c6d616e7a616661723934392f6c61726176656c2d6170692d657863657074696f6e732d68616e646c6572)[![Total Downloads](https://camo.githubusercontent.com/f25b6e87b73912cff6f84bcf126363f4093f52df18f361d0088489c0475aad37/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f73616c6d616e7a616661722f6c61726176656c2d6170692d657863657074696f6e732d68616e646c65723f7374796c653d666c61742d737175617265)](https://packagist.org/packages/salmanzafar/laravel-jwt-auto-installer)

A Laravel package that makes it easy to handle and customize api exceptions and responses and also support for model uuid

Table of contents
-----------------

[](#table-of-contents)

- [Installation](#installation)
- [Configuration](#configuration-optional)
- [Api Exceptions](#api-exceptions)
- [Custom Api Responses](#custom-api-responses)
- [Model Uuid](#model-uuid)
- [Publishing files / configurations](#publishing-files)

Installation
============

[](#installation)

```
composer require salmanzafar/laravel-api-exceptions-handler --dev
```

Configuration (optional)
------------------------

[](#configuration-optional)

Publishing service provider

```
php artisan vendor:publish --provider="Salman\ApiExceptionHandler\ApiExceptionHandlerServiceProvider::class"
```

Enable the package (Optional)
-----------------------------

[](#enable-the-package-optional)

This package implements Laravel auto-discovery feature. After you install it the package provider and facade are added automatically for laravel &gt;= 5.5.

Api Exceptions
==============

[](#api-exceptions)

There are many cases in which you want to return a custom response to an api instead of a default response

For example you have a api to get user and you want to return a custom response instead of laravel default response

```
public function getUser(User $user)
{
  return $user;
}
```

in above case by default laravel will throw 404 exception if user is not found. now let's see how we can customize this:

Go to `app\Exceptions\Handler.php` and copy and paste the below part

```
namespace App\Exceptions;

use Exception;
use App\Exceptions\ExceptionTrait;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
     use ExceptionTrait;

     /**
      * A list of the exception types that are not reported.
      *
      * @var array
      */
     protected $dontReport = [
         //
     ];

     /**
      * A list of the inputs that are never flashed for validation exceptions.
      *
      * @var array
      */
     protected $dontFlash = [
         'password',
         'password_confirmation',
     ];

     /**
      * Report or log an exception.
      *
      * @param  \Exception  $exception
      * @return void
      */
     public function report(Exception $exception)
     {
         parent::report($exception);
     }

     /**
      * Render an exception into an HTTP response.
      *
      * @param  \Illuminate\Http\Request  $request
      * @param  \Exception  $exception
      * @return \Illuminate\Http\Response
      */
     public function render($request, Exception $exception)
     {
         if ($request->expectsJson())
         {
             return $this->ApiExceptions($request,$exception);
         }

         return parent::render($request, $exception);
     }
}
```

- `ExceptionTrait` has all the exceptions handling
- In `render function` we've added a conditional check add then returned `apiExceptions`

Now it'll return a the json customized response instead of default laravel response (see below):

```
{
   "error": "Model Not found"
}
```

### Custom Api Responses

[](#custom-api-responses)

There are plenty of cases where ypu want to customize the laravel default validation/form request response so in thise cas this will help you customizing your response

Let's see the default validation response for laravel

```
{
  "message": "The given data is invalid",
  "errors": {
      "name": ['name is require']
    }

}
```

Now Let's modify this by creating a form request e.g `CarRequest.php`

```
