From 27d504079a5460100d50946cb8455b1738a816f1 Mon Sep 17 00:00:00 2001 From: Bas van den Aakster Date: Sun, 14 Sep 2025 20:32:23 +0200 Subject: [PATCH] Fix critical error handling and race condition issues MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 🔴 Critical fixes: - Fix race condition: processImmediately now properly fails if job creation fails - Fix silent job failures: job creation failures now throw errors instead of warnings - Ensure atomic operations: either email + job succeed together, or both fail ⚠️ Improvements: - Simplify error handling in processEmailJob to be more consistent - Add proper validation for missing PayloadCMS jobs configuration - Make error messages more descriptive and actionable --- src/jobs/processEmailJob.ts | 8 +----- src/sendEmail.ts | 56 +++++++++++++++++++++---------------- 2 files changed, 33 insertions(+), 31 deletions(-) diff --git a/src/jobs/processEmailJob.ts b/src/jobs/processEmailJob.ts index ddfd766..39137ae 100644 --- a/src/jobs/processEmailJob.ts +++ b/src/jobs/processEmailJob.ts @@ -64,13 +64,7 @@ export const processEmailJob = { } } } catch (error) { - // Re-throw Error instances to preserve stack trace and error context - if (error instanceof Error) { - throw error - } else { - // Only wrap non-Error values - throw new Error(`Failed to process email ${emailId}: ${String(error)}`) - } + throw new Error(`Failed to process email ${emailId}: ${error instanceof Error ? error.message : String(error)}`) } } } diff --git a/src/sendEmail.ts b/src/sendEmail.ts index c6e95ef..3b3cafd 100644 --- a/src/sendEmail.ts +++ b/src/sendEmail.ts @@ -145,36 +145,44 @@ export const sendEmail = async