Giter VIP home page Giter VIP logo

laravel-api-error-handler's Introduction

Laravel Api Error Handler

a useful package for handling exception in laravel.

๐Ÿ“ฅ Installation

you can install this package via Composer:

composer require hamidreza2005/laravel-api-error-handler

and after installation you can run following command to publish config files

php artisan vendor:publish --tag laravel-api-error-handler

โš™๏ธ Configuration

to configure this package go to config/api-error-handler.php

<?php  
  
return [  
    /*
     * this is where you define which handler deal with which errors. each handler can handle multiple errors
     */

    "handlers" =>[
        NotFoundExceptionHandler::class => [
            "Symfony\Component\HttpKernel\Exception\NotFoundHttpException",
            "Symfony\Component\HttpKernel\Exception\MethodNotAllowedHttpException"
        ],
        ServerInternalExceptionHandler::class => [
            "ErrorException",
            "Illuminate\Database\QueryException"
        ],
        AccessDeniedExceptionHandler::class => [
            "Illuminate\Auth\AuthenticationException",
            "Symfony\Component\HttpKernel\Exception\HttpException"
        ],
        ValidationExceptionHandler::class => [
            "Illuminate\Validation\ValidationException"
        ],
    ],

    /*
     * if the app is not in debug mode. all unknown exceptions will be handled by this.
     */
    "internal_error_handler" => ServerInternalExceptionHandler::class,
];

this package provides some handlers for common exceptions like ModelNotFound But if you want to customize it you can do like this :

<?php  
  
return [  
  YourHandler::class => [
        // exceptions
    ],   
];
Class Status Code Message
NotFoundException 404 Not Found
ServerInternalException 500 Server Internal Error
AccessDeniedException 403 Access Denied
ValidationException 422 all validation errors
DefaultException the status code of the Exception the message of the Exception

๐Ÿš€ how to let handlers do their job ?

add ApiErrorHandler trait to ExceptionHandler located in app\Exceptions\Handler.php:

<?php  
  
namespace App\Exceptions;  
  
use hamidreza2005\LaravelApiErrorHandler\Traits\ApiErrorHandler;  
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;  
use Throwable;  
  
class Handler extends ExceptionHandler  
{  
	  use ApiErrorHandler;  
	  /**  
	 * 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',  
	 ];  
	  /**  
	 * Register the exception handling callbacks for the application. * * @return void  
	 */
	 public function register()  
	 {
	   //  
	 }  
  
	 public function render($request, Throwable $e)  
	 {
		  return $this->handle($this->prepareException($e));  
	 }
 }

Make Your Own Exception Handler!

if you want to make your own handler your class has to extend hamidreza2005\LaravelApiErrorHandler\Handlers\ExceptionHandler

i.e:

<?php  

class MyException extends ExceptionHandler  
{  
	  public function handleStatusCode():void  
	  {  
		  $this->statusCode = 499;  
	  }
	   
	  public function handleMessage():void  
	  {  
		  $this->message = "My Message";  
	  }
 }

๐Ÿ“œ License

The MIT License (MIT). Please see License File for more information.


๐Ÿ™‹ Contributing

If you find an issue, or have a better way to do something, feel free to open an issue , or a pull request.

laravel-api-error-handler's People

Contributors

hamidreza2005 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.