added exception handling
This commit is contained in:
@@ -2,13 +2,17 @@
|
|||||||
|
|
||||||
namespace App\Exceptions;
|
namespace App\Exceptions;
|
||||||
|
|
||||||
use App\Enum\HttpResponseCodes;
|
use App\Mail\ExceptionMail;
|
||||||
use Exception;
|
|
||||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
use PDOException;
|
use PDOException;
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
use Symfony\Component\ErrorHandler\ErrorRenderer\HtmlErrorRenderer;
|
||||||
|
use Exception;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
|
use Illuminate\Support\Facades\Mail;
|
||||||
|
use Symfony\Component\ErrorHandler\Exception\FlattenException;
|
||||||
|
|
||||||
class Handler extends ExceptionHandler
|
class Handler extends ExceptionHandler
|
||||||
{
|
{
|
||||||
@@ -58,8 +62,42 @@ class Handler extends ExceptionHandler
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
$this->renderable(
|
||||||
|
function (Throwable $exception, $request) {
|
||||||
|
if ($exception instanceof \Illuminate\Session\TokenMismatchException) {
|
||||||
|
return redirect()
|
||||||
|
->back()
|
||||||
|
->withInput($request->except('password'))
|
||||||
|
->with('errorMessage', 'This form has expired due to inactivity. Please try again.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
$this->reportable(function (Throwable $e) {
|
$this->reportable(function (Throwable $e) {
|
||||||
//
|
if ($this->shouldReport($e) === true) {
|
||||||
|
$this->sendEmail($e);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function sendEmail(Throwable $exception)
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$e = FlattenException::create($exception);
|
||||||
|
$handler = new HtmlErrorRenderer(true);
|
||||||
|
$css = $handler->getStylesheet();
|
||||||
|
$content = $handler->getBody($e);
|
||||||
|
|
||||||
|
Mail::send('emails.exception', compact('css', 'content'), function ($message) {
|
||||||
|
$message
|
||||||
|
->to('youremailhere@gmail.com')
|
||||||
|
->subject('Exception Generated')
|
||||||
|
;
|
||||||
|
});
|
||||||
|
} catch (Throwable $ex) {
|
||||||
|
Log::error($ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
55
app/Mail/ExceptionMail.php
Normal file
55
app/Mail/ExceptionMail.php
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Mail;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Mail\Mailable;
|
||||||
|
use Illuminate\Mail\Mailables\Content;
|
||||||
|
use Illuminate\Mail\Mailables\Envelope;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
|
||||||
|
class ExceptionMail extends Mailable
|
||||||
|
{
|
||||||
|
use Queueable;
|
||||||
|
use SerializesModels;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new message instance.
|
||||||
|
*/
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
//
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message envelope.
|
||||||
|
*/
|
||||||
|
public function envelope(): Envelope
|
||||||
|
{
|
||||||
|
return new Envelope(
|
||||||
|
subject: 'Exception Mail',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message content definition.
|
||||||
|
*/
|
||||||
|
public function content(): Content
|
||||||
|
{
|
||||||
|
return new Content(
|
||||||
|
view: 'view.name',
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the attachments for the message.
|
||||||
|
*
|
||||||
|
* @return array<int, \Illuminate\Mail\Mailables\Attachment>
|
||||||
|
*/
|
||||||
|
public function attachments(): array
|
||||||
|
{
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
}
|
||||||
10
resources/views/emails/exception.blade.php
Normal file
10
resources/views/emails/exception.blade.php
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<style>{!! $css ?? '' !!}</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
{!! $content ?? '' !!}
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user