From 91eb052c229e684162145f4403c0dc49f4340535 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 24 Feb 2016 20:13:55 +0100 Subject: [PATCH] Use a job to send the error email instead of inline. #196 --- app/Exceptions/Handler.php | 44 ++++-------- app/Jobs/MailError.php | 93 ++++++++++++++++++++++++++ resources/views/emails/error-html.twig | 2 +- resources/views/emails/error.twig | 2 +- 4 files changed, 109 insertions(+), 32 deletions(-) create mode 100644 app/Jobs/MailError.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 99e2dbdaac..dd919b5dab 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -5,14 +5,12 @@ namespace FireflyIII\Exceptions; use Auth; use ErrorException; use Exception; +use FireflyIII\Jobs\MailError; +use FireflyIII\User; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; -use Illuminate\Mail\Message; -use Log; -use Mail; use Request; -use Swift_TransportException; use Symfony\Component\HttpKernel\Exception\HttpException; /** @@ -69,34 +67,20 @@ class Handler extends ExceptionHandler if ($exception instanceof FireflyException || $exception instanceof ErrorException) { - // mail? - try { - $email = env('SITE_OWNER'); - $user = Auth::user(); - $args = [ - 'errorMessage' => $exception->getMessage(), - 'stacktrace' => $exception->getTraceAsString(), - 'file' => $exception->getFile(), - 'line' => $exception->getLine(), - 'code' => $exception->getCode(), - 'loggedIn' => !is_null($user), - 'user' => $user, - 'ip' => Request::ip(), + $user = Auth::check() ? Auth::user() : new User; - ]; + $data = [ + 'class' => get_class($exception), + 'errorMessage' => $exception->getMessage(), + 'stackTrace' => $exception->getTraceAsString(), + 'file' => $exception->getFile(), + 'line' => $exception->getLine(), + 'code' => $exception->getCode(), + ]; - Mail::send( - ['emails.error-html', 'emails.error'], $args, - function (Message $message) use ($email) { - if ($email != 'mail@example.com') { - $message->to($email, $email)->subject('Caught an error in Firely III.'); - } - } - ); - } catch (Swift_TransportException $e) { - // could also not mail! :o - Log::error($e->getMessage()); - } + // create job that will mail. + $job = new MailError($user, env('SITE_OWNER'), Request::ip(), $data); + dispatch($job); } parent::report($exception); diff --git a/app/Jobs/MailError.php b/app/Jobs/MailError.php new file mode 100644 index 0000000000..ff5cd94a4d --- /dev/null +++ b/app/Jobs/MailError.php @@ -0,0 +1,93 @@ +user = $user; + $this->destination = $destination; + $this->ip = $ip; + $this->exception = $exceptionData; + + Log::debug('In mail job constructor'); + } + + /** + * Execute the job. + * + * @return void + */ + public function handle() + { + Log::debug('Start of handle()'); + if ($this->attempts() < 3) { + // mail? + try { + $email = env('SITE_OWNER'); + $args = [ + 'class' => $this->exception['class'], + 'errorMessage' => $this->exception['errorMessage'], + 'stacktrace' => $this->exception['stackTrace'], + 'file' => $this->exception['file'], + 'line' => $this->exception['line'], + 'code' => $this->exception['code'], + 'loggedIn' => !is_null($this->user->id), + 'user' => $this->user, + 'ip' => $this->ip, + + ]; + + Mail::send( + ['emails.error-html', 'emails.error'], $args, + function (Message $message) use ($email) { + if ($email != 'mail@example.com') { + $message->to($email, $email)->subject('Caught an error in Firely III.'); + } + } + ); + } catch (Swift_TransportException $e) { + // could also not mail! :o + Log::error('Swift Transport Exception' . $e->getMessage()); + } catch (ErrorException $e) { + Log::error('ErrorException ' . $e->getMessage()); + } + Log::debug('Successfully handled error.'); + } + } +} diff --git a/resources/views/emails/error-html.twig b/resources/views/emails/error-html.twig index aeb6f6c040..828d354812 100644 --- a/resources/views/emails/error-html.twig +++ b/resources/views/emails/error-html.twig @@ -6,7 +6,7 @@

- Firefly III ran into an error: {{ errorMessage }}. + Firefly III ran into an error: {{ errorMessage }} of class (type) "{{ class }}".

This error occured in file {{ file }} on line {{ line }} with code {{ code }}. diff --git a/resources/views/emails/error.twig b/resources/views/emails/error.twig index 19dbd5d9aa..cea37a1d82 100644 --- a/resources/views/emails/error.twig +++ b/resources/views/emails/error.twig @@ -1,4 +1,4 @@ -Firefly III ran into an error: {{ errorMessage }}. +Firefly III ran into an error: {{ errorMessage }} of class (type) "{{ class }}". This error occured in file "{{ file }}" on line {{ line }} with code {{ code }}.