mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-10-12 15:35:15 +00:00
Use a job to send the error email instead of inline. #196
This commit is contained in:
@@ -5,14 +5,12 @@ namespace FireflyIII\Exceptions;
|
|||||||
use Auth;
|
use Auth;
|
||||||
use ErrorException;
|
use ErrorException;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use FireflyIII\Jobs\MailError;
|
||||||
|
use FireflyIII\User;
|
||||||
use Illuminate\Auth\Access\AuthorizationException;
|
use Illuminate\Auth\Access\AuthorizationException;
|
||||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||||
use Illuminate\Mail\Message;
|
|
||||||
use Log;
|
|
||||||
use Mail;
|
|
||||||
use Request;
|
use Request;
|
||||||
use Swift_TransportException;
|
|
||||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -69,34 +67,20 @@ class Handler extends ExceptionHandler
|
|||||||
|
|
||||||
if ($exception instanceof FireflyException || $exception instanceof ErrorException) {
|
if ($exception instanceof FireflyException || $exception instanceof ErrorException) {
|
||||||
|
|
||||||
// mail?
|
$user = Auth::check() ? Auth::user() : new User;
|
||||||
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(),
|
|
||||||
|
|
||||||
];
|
$data = [
|
||||||
|
'class' => get_class($exception),
|
||||||
|
'errorMessage' => $exception->getMessage(),
|
||||||
|
'stackTrace' => $exception->getTraceAsString(),
|
||||||
|
'file' => $exception->getFile(),
|
||||||
|
'line' => $exception->getLine(),
|
||||||
|
'code' => $exception->getCode(),
|
||||||
|
];
|
||||||
|
|
||||||
Mail::send(
|
// create job that will mail.
|
||||||
['emails.error-html', 'emails.error'], $args,
|
$job = new MailError($user, env('SITE_OWNER'), Request::ip(), $data);
|
||||||
function (Message $message) use ($email) {
|
dispatch($job);
|
||||||
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());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parent::report($exception);
|
parent::report($exception);
|
||||||
|
93
app/Jobs/MailError.php
Normal file
93
app/Jobs/MailError.php
Normal file
@@ -0,0 +1,93 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace FireflyIII\Jobs;
|
||||||
|
|
||||||
|
use ErrorException;
|
||||||
|
use FireflyIII\User;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Mail\Message;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Log;
|
||||||
|
use Mail;
|
||||||
|
use Swift_TransportException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class MailError
|
||||||
|
*
|
||||||
|
* @package FireflyIII\Jobs
|
||||||
|
*/
|
||||||
|
class MailError extends Job implements ShouldQueue
|
||||||
|
{
|
||||||
|
use InteractsWithQueue, SerializesModels;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
|
protected $destination;
|
||||||
|
/** @var array */
|
||||||
|
protected $exception;
|
||||||
|
/** @var string */
|
||||||
|
protected $ip;
|
||||||
|
/** @var User */
|
||||||
|
protected $user;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* MailError constructor.
|
||||||
|
*
|
||||||
|
* @param User $user
|
||||||
|
* @param string $destination
|
||||||
|
* @param string $ip
|
||||||
|
* @param array $exception
|
||||||
|
*/
|
||||||
|
public function __construct(User $user, string $destination, string $ip, array $exceptionData)
|
||||||
|
{
|
||||||
|
$this->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.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@@ -6,7 +6,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
|
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
|
||||||
Firefly III ran into an error: <span style="font-family: monospace;">{{ errorMessage }}</span>.
|
Firefly III ran into an error: <span style="font-family: monospace;">{{ errorMessage }}</span> of class (type) "{{ class }}".
|
||||||
</p>
|
</p>
|
||||||
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
|
<p style="font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size:13px;">
|
||||||
This error occured in file <span style="font-family: monospace;">{{ file }}</span> on line {{ line }} with code {{ code }}.
|
This error occured in file <span style="font-family: monospace;">{{ file }}</span> on line {{ line }} with code {{ code }}.
|
||||||
|
@@ -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 }}.
|
This error occured in file "{{ file }}" on line {{ line }} with code {{ code }}.
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user