Author

Exception Handling in Laravel

Image

by Samarth Sinha on 2017-05-22 00:41:32

Error and exception handling is already configured in laravel project. The App\Exceptions\Handler class is where all exceptions triggered by your application are logged and then rendered back to the user. To send an email when an exception comes write the following codes in your App\Exceptions\Handler class in the report():

public function report(Exception $exception)
{
        if ($exception instanceof \Exception) {
            $excep = [];
            $excep['code'] = $exception->getCode();
            $excep['file'] = $exception->getFile();
            $excep['line'] = $exception->getLine();
            $excep['message'] = $exception->getMessage();
            $excep['trace'] = $exception->getTraceAsString();
            // emails.exception is the template of your email
            // it will have access to the $error that we are passing below
            Mail::send('view_file_in_resources', ['errors' => $excep], function ($m) {
                $m->to('sender@email.com', 'Sender Name')->subject('Subject');
            });
        }

        parent::report($exception);
}

Viewer Comments (2)

About the Author

  • avatar
    Samarth Sinha

    My name Samarth Sinha. I work on developing Web Applications using PHP and MySQL and in different frameworks of PHP like Laravel, CakePHP, CodeIgnitor and Smarty. In databases I have worked in MySQL and Postgres. I started this blog to share my knowledge to those who wants to start developing Web Applications using PHP.