1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Traits; |
4
|
|
|
|
5
|
|
|
use App\Mail\ExceptionOccured; |
6
|
|
|
use Illuminate\Support\Facades\Log; |
7
|
|
|
use Mail; |
8
|
|
|
use Symfony\Component\Debug\Exception\FlattenException; |
9
|
|
|
use Symfony\Component\ErrorHandler\ErrorHandler as SymfonyExceptionHandler; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
trait ExceptionNotificationHandlerTrait |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* A list of the exception types that should not be reported. |
16
|
|
|
* |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $dontReport = [ |
20
|
|
|
\Illuminate\Auth\AuthenticationException::class, |
21
|
|
|
\Illuminate\Auth\Access\AuthorizationException::class, |
22
|
|
|
\Symfony\Component\HttpKernel\Exception\HttpException::class, |
23
|
|
|
\Illuminate\Database\Eloquent\ModelNotFoundException::class, |
24
|
|
|
\Illuminate\Session\TokenMismatchException::class, |
25
|
|
|
\Illuminate\Validation\ValidationException::class, |
26
|
|
|
]; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Report or log an exception. |
30
|
|
|
* |
31
|
|
|
* This is a great spot to send exceptions to Sentry, Bugsnag, etc. |
32
|
|
|
* |
33
|
|
|
* @param \Throwable $exception |
34
|
|
|
* |
35
|
|
|
* @return void |
36
|
|
|
*/ |
37
|
|
|
public function report(Throwable $exception) |
38
|
|
|
{ |
39
|
|
|
$enableEmailExceptions = config('exceptions.emailExceptionEnabled'); |
40
|
|
|
|
41
|
|
|
if ($enableEmailExceptions === '') { |
42
|
|
|
$enableEmailExceptions = config('exceptions.emailExceptionEnabledDefault'); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ($enableEmailExceptions) { |
46
|
|
|
if ($this->shouldReport($exception)) { |
|
|
|
|
47
|
|
|
$this->sendEmail($exception); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
parent::report($exception); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Sends an email upon exception. |
56
|
|
|
* |
57
|
|
|
* @param \Throwable $exception |
58
|
|
|
* |
59
|
|
|
* @return void |
60
|
|
|
*/ |
61
|
|
|
public function sendEmail(Throwable $exception) |
62
|
|
|
{ |
63
|
|
|
try { |
64
|
|
|
$e = FlattenException::create($exception); |
65
|
|
|
$handler = new SymfonyExceptionHandler(); |
66
|
|
|
$html = $handler->getHtml($e); |
|
|
|
|
67
|
|
|
|
68
|
|
|
Mail::send(new ExceptionOccured($html)); |
69
|
|
|
} catch (Throwable $exception) { |
70
|
|
|
Log::error($exception); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|