add Mail Error handler

This commit is contained in:
Jérémy Munsch 2021-05-21 14:23:34 +02:00
parent 28e42106d7
commit 8b5e28bb92
5 changed files with 74 additions and 0 deletions

View file

@ -32,6 +32,7 @@ MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
MAIL_FROM_ADDRESS=null
MAIL_FROM_NAME="${APP_NAME}"
MAIL_LIST_SERVICEWEB=informatique.internet@ldc.fr
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=

View file

@ -3,9 +3,13 @@
namespace App\Exceptions;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Support\Facades\Mail;
use RuntimeException;
use Throwable;
class Handler extends ExceptionHandler
{
/**
* A list of the exception types that are not reported.
*
@ -25,6 +29,45 @@ class Handler extends ExceptionHandler
'password_confirmation',
];
/**
* Report or log an exception.
*
* @param \Throwable $exception
* @return void
*
* @throws \Exception
*/
public function report(Throwable $e)
{
if (get_class($e) == RedirectTo::class) {
abort(redirect($e->url));
}
try {
if (config('app.debug') === false) {
if ($this->shouldntReport($e)) {
return;
}
Mail::send([
'html' => 'emails.exception',
'text' => 'emails.exception-plain'
], [
'exception' => $e,
'appName' => config('app.name')
], function ($m) {
$m->to(
config('mail.contactsList.serviceweb'),
sprintf('%s Exception Handler', config('app.name'))
)->subject('Need help !, an error occured in your application, please check Logs ASAP');
});
}
// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedCatch
} catch (RuntimeException $e) {
// do nothing, mail send error, let laravel only handle this
} finally {
parent::report($e);
}
}
/**
* Register the exception handling callbacks for the application.
*

View file

@ -91,6 +91,10 @@ return [
'name' => env('MAIL_FROM_NAME', 'Example'),
],
'contactsList' => [
'serviceweb' => env('MAIL_LIST_SERVICEWEB', 'informatique.internet@ldc.fr')
],
/*
|--------------------------------------------------------------------------
| Markdown Mail Settings

View file

@ -0,0 +1,10 @@
{{ $appName }} Exception Handler
Une erreur est survenue sur le site {{ $appName }}
{{ $exception->getMessage() }}
date : {{ date('Y-m-d H:i:s') }}
code : {{$exception->getCode() }}
file : {{$exception->getFile() }}
line : {{$exception->getLine() }}
Veuillez vérifier les logs pour en voir la trace.

View file

@ -0,0 +1,16 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>{{ $appName }} Exception Handler</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<h1>Une erreur est survenue sur le site {{ $appName }}</h1>
<h2>{{ $exception->getMessage() }}</h2>
<p>date : {{ date('Y-m-d H:i:s') }}</p>
<p>code : {{$exception->getCode() }}</p>
<p>file : {{$exception->getFile() }}</p>
<p>line : {{$exception->getLine() }}</p>
<p>Veuillez vérifier les logs pour en voir la trace.</p>
</body>
</html>