Laravel CustomException class in laravel


Example

all errors and exceptions, both custom and default, are handled by the Handler class in app/Exceptions/Handler.php with the help of two methods.

  • report()

  • render()

    public function render($request, Exception $e)
    {
     //check if exception is an instance of ModelNotFoundException.
     if ($e instanceof ModelNotFoundException)
     {
         // ajax 404 json feedback
         if ($request->ajax())
         {
             return response()->json(['error' => 'Not Found'], 404);
         }
         // normal 404 view page feedback
         return response()->view('errors.missing', [], 404);
      }
       return parent::render($request, $e);
    }
    

then create view related to error in errors folder named 404.blade.php

User not found.

You broke the balance of the internet