Skip to content

Commit 8d6a1eb

Browse files
committed
"Refactor error handling in NotifyAdminOfError listener
This commit eliminates an unnecessary newline, adds an initial value of 0 to the `$coolDownPeriod` variable to avoid uninitialized variable errors, and refines the process of error hashing and mailing in context of exception existence. Now, the code attempts to create an `errorHash` only if there is an exception in the event context, reducing the likelihood of errors when context does not include an exception."
1 parent 5bc9e19 commit 8d6a1eb

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

src/Listeners/NotifyAdminOfError.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace Hugomyb\ErrorMailer\Listeners;
44

55

6-
76
use Hugomyb\ErrorMailer\Notifications\ErrorOccurred;
87
use Illuminate\Support\Facades\Cache;
98
use Illuminate\Support\Facades\Config;
@@ -29,19 +28,21 @@ public function __construct()
2928
*/
3029
public function handle($event)
3130
{
31+
$coolDownPeriod = 0;
32+
3233
if (!in_array(env('APP_ENV'), config('error-mailer.disabledOn'))) {
3334

3435
$recipient = config()->has('error-mailer.email.recipient')
3536
? config('error-mailer.email.recipient')
3637
3738

38-
$errorHash = md5($event->context['exception']->getMessage() . $event->context['exception']->getFile());
39+
if (isset($event->context['exception'])) {
40+
$errorHash = md5($event->context['exception']->getMessage() . $event->context['exception']->getFile());
3941

40-
$cacheKey = 'error_mailer_' . $errorHash;
41-
$coolDownPeriod = 15;
42+
$cacheKey = 'error_mailer_' . $errorHash;
43+
$coolDownPeriod = 15;
4244

43-
if (!Cache::has($cacheKey)) {
44-
if (isset($event->context['exception'])) {
45+
if (!Cache::has($cacheKey)) {
4546
Mail::to($recipient)->send(new ErrorOccurred($event->context['exception']));
4647
Cache::put($cacheKey, true, now()->addMinutes($coolDownPeriod));
4748
}

0 commit comments

Comments
 (0)