added exception handling
This commit is contained in:
@@ -2,13 +2,17 @@
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Enum\HttpResponseCodes;
|
||||
use Exception;
|
||||
use App\Mail\ExceptionMail;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
||||
use Throwable;
|
||||
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
|
||||
{
|
||||
@@ -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) {
|
||||
//
|
||||
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 [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user