Creating a custom exception class and custom handler class in Laravel 5.3

In new versions of Laravel, you can create a custom handler using this command:

php artisan make:exception CustomException

Then you should call these methods "report(), render()" inside your custom handler and they will override the existing ones in the App\Exceptions\Handler.

report() used if you want to log the errors.

render() used if you want to redirect back with error or return HTTP response (like your own Blade file) or if you're building an API.

For more information, you can check Laravel documentation.


Laravel calls the render function of App\Exceptions\Handler class. So overriding it will not work.

You have to add it in App\Exceptions\Handler class only.

For example:

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Auth\AuthenticationException;
use App\Project\Frontend\Repo\Vehicle\EloquentVehicle;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;

class Handler extends ExceptionHandler
{
    /**
     * A list of the exception types that should not be reported.
     *
     * @var array
     */
    protected $dontReport = [
        \Illuminate\Auth\AuthenticationException::class,
        \Illuminate\Auth\Access\AuthorizationException::class,
        \Symfony\Component\HttpKernel\Exception\HttpException::class,
        \Illuminate\Database\Eloquent\ModelNotFoundException::class,
        \Illuminate\Session\TokenMismatchException::class,
        \Illuminate\Validation\ValidationException::class,
    ];

    /**
     * Report or log an exception.
     *
     * This is a great spot to send exceptions to Sentry, Bugsnag, etc.
     *
     * @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($exception instanceof CustomException) {
            return $this->showCustomErrorPage();
        }

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

    protected function showCustomErrorPage()
    {
        $recentlyAdded = app(EloquentVehicle::class)->fetchLatestVehicles(0, 12);

        return view()->make('errors.404Custom')->with('recentlyAdded', $recentlyAdded);
    }
}

Step 1: Create a custom Exception

php artisan make:exception CustomException

Step 2: Include that exception in your code

use App\Exceptions\CustomException;

Pass your error to that Exception

if($somethingerror){
  throw new CustomException('Your error message');          
}

Step 3: Handle your exception in CustomException file in report() and render() methods

For example, if I want display the error as JSON format

<?php
namespace App\Exceptions;
use Exception;
class CustomException extends Exception
{   
    public function render($request)
    {       
        return response()->json(["error" => true, "message" => $this->getMessage()]);       
    }
}