From 99651bb61e4ca18ce50d9ab9809d6d9e01f7051b Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 26 Oct 2025 09:53:19 +0100 Subject: [PATCH] Catch some exceptions. --- app/Jobs/MailError.php | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/app/Jobs/MailError.php b/app/Jobs/MailError.php index ecc8c24b5d..b6f3c62416 100644 --- a/app/Jobs/MailError.php +++ b/app/Jobs/MailError.php @@ -31,6 +31,8 @@ use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Mail; +use Safe\Exceptions\FilesystemException; +use Safe\Exceptions\JsonException; use Symfony\Component\Mailer\Exception\TransportException; use function Safe\file_get_contents; @@ -125,11 +127,23 @@ class MailError extends Job implements ShouldQueue if (!file_exists($file)) { Log::debug(sprintf('Wrote new file in "%s"', $file)); - file_put_contents($file, json_encode($limits, JSON_PRETTY_PRINT)); + try { + file_put_contents($file, json_encode($limits, JSON_PRETTY_PRINT)); + } catch (FilesystemException $e) { + Log::warning(sprintf('[a] Could not write file "%s": %s', $file, $e->getMessage())); + } catch (JsonException $e) { + Log::warning(sprintf('[b] Could not parse file "%s": %s', $file, $e->getMessage())); + } } if (file_exists($file)) { Log::debug(sprintf('Read file in "%s"', $file)); - $limits = json_decode(file_get_contents($file), true); + try { + $limits = json_decode(file_get_contents($file), true); + } catch (FilesystemException $e) { + Log::warning(sprintf('[c] Could not read file "%s": %s', $file, $e->getMessage())); + } catch (JsonException $e) { + Log::warning(sprintf('[d] Could not parse file "%s": %s', $file, $e->getMessage())); + } } // limit reached? foreach ($types as $type => $info) { @@ -157,7 +171,13 @@ class MailError extends Job implements ShouldQueue } ++$limits[$type]['sent']; } - file_put_contents($file, json_encode($limits, JSON_PRETTY_PRINT)); + try { + file_put_contents($file, json_encode($limits, JSON_PRETTY_PRINT)); + } catch (FilesystemException $e) { + Log::warning(sprintf('[c] Could not write file "%s": %s', $file, $e->getMessage())); + } catch (JsonException $e) { + Log::warning(sprintf('[c] Could not parse file "%s": %s', $file, $e->getMessage())); + } Log::debug('No limits reached, return FALSE.'); return false;