When our application is in production, if we want to know exceptions then we will have to wait for the Customers to report it or we will have to check the Log file regularly. It would help a lot if we can know instantly when an exception happens. Well we can do that in Laravel. Even we can send an Email to the User about the Issue. Usually Customers happy when they know "Help is on the way".
There is a global Exception handler located at app/Exceptions/Handler.php. It has two functions report and render. In report we can send mail to the concerned Person(Developer) and to the Customer. In render we can redirect to an Error page.
Sometimes we don't want to perform this operation for some type of Exceptions. So we will have to filter out. Mine looks like this:
public function report(Exception $e)
{
//Don't fire mail in case of bad link
if (!($e instanceof NotFoundHttpException)) {
HandlerService::sendMailOnException($e);
}
return parent::report($e);
}
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException) {
return response()->make(view('errors.404'), 404);
} elseif ($e instanceof ReflectionException) {
return response()->make(view('errors.500'), 500);
}
return parent::render($request, $e);
}
Hope this helps.
Thanks
There is a global Exception handler located at app/Exceptions/Handler.php. It has two functions report and render. In report we can send mail to the concerned Person(Developer) and to the Customer. In render we can redirect to an Error page.
Sometimes we don't want to perform this operation for some type of Exceptions. So we will have to filter out. Mine looks like this:
public function report(Exception $e)
{
//Don't fire mail in case of bad link
if (!($e instanceof NotFoundHttpException)) {
HandlerService::sendMailOnException($e);
}
return parent::report($e);
}
public function render($request, Exception $e)
{
if($e instanceof NotFoundHttpException) {
return response()->make(view('errors.404'), 404);
} elseif ($e instanceof ReflectionException) {
return response()->make(view('errors.500'), 500);
}
return parent::render($request, $e);
}
Hope this helps.
Thanks
No comments:
Post a Comment