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