diff --git a/controllers/bulkApiMailer.js b/controllers/bulkApiMailer.js index bb8ced4..6ba8b2d 100644 --- a/controllers/bulkApiMailer.js +++ b/controllers/bulkApiMailer.js @@ -39,12 +39,27 @@ bulkQueue.process(async (job) => { }); if (!response.ok) { - console.error(`Bulk API failed with status: ${response.status}`); - console.error(`HTTP Error Status: ${response.statusText}`) - console.error(response) + console.error(`Bulk API failed with status: ${response.status} - HTTP Error Status: ${response.statusText}`); + let errorDetails = {}; + try { + errorDetails = await response.json(); + } catch (parseErr) { + console.error('Failed to parse error body:', parseErr); + } + + const err = new Error(`GC Notify Bulk API error: ${response.status} ${response.statusText}`); + + // Attach status + parsed body to error object + err.httpStatus = response.status; + err.statusText = response.statusText; + err.errorBody = errorDetails?.errors?.[0] || errorDetails || errorDetails.toString(); + err.errorCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null; + err.errorMessage = errorDetails?.errors?.[0]?.message || errorDetails?.message ||response.statusText; - throw new Error(`HTTP Error Status: ${response.status}`); - } else { + console.error(` --------------------> GC Notify Bulk API error: `); + console.error(err); + throw err; + } else { jobSuccess = true; // If request is successful, update mailing status mailingManager.mailingUpdate(jobData.mailingId, mailingState.sent, { historyState: mailingState.sending }); @@ -52,12 +67,14 @@ bulkQueue.process(async (job) => { } } catch ( error ) { - console.error("bulk q process error") - console.log(error) - const currDate = new Date(), currDateTime = currDate.getTime(); + const httpStatus = error.httpStatus || null; + const errCode = error.errorCode || error.code || null; + const errMsg = error.errorMessage || error.message; + const errDetails = error.errorBody ? JSON.stringify(error.errorBody) : error.toString(); + // Connect to MongoDB mongoInstance = await MongoClient.connect(process.env.MONGODB_URI || '', { useUnifiedTopology: true }); dbConn = mongoInstance.db(process.env.MONGODB_NAME || 'subs'); @@ -66,17 +83,17 @@ bulkQueue.process(async (job) => { if ( dbConn ) { try { await dbConn.collection("notify_logs").insertOne({ - createdAt: currDate, - jobData: job.data, - err_msg: error.message, - err_status: error.status, - err_code: error.code, - error: error.toString(), - emailLength: emailLength + createdAt: currDate, + jobData: job.data, + errorMessage: errMsg, // GCNotify message + http_status: httpStatus, // HTTP status + notify_Errorcode: errCode, // GC Notify error code or Node error.code + errDetails: errDetails, // full JSON/error string + emailLength: emailLength, }); } catch ( dbError ) { console.error( "Failed to log error in notify_logs:", dbError ); - throw new Error ( "Bulk Queue process DB error " + dbError ) + throw new Error ( "Bulk Queue process DB error " + dbError.message ) } } @@ -84,7 +101,7 @@ bulkQueue.process(async (job) => { // Try to email us (only with the predefined interval) // if ( _notifyUsNotBeforeTimeLimit <= currDateTime ) { - letUsKnow( "Bulk Queue error", { + letUsKnow( "Bulk Queue error " + error.message + " - " + errMsg, { type: "bulk_q_process_error", currTime: currDateTime, lastTime: _notifyUsNotBeforeTimeLimit @@ -96,8 +113,9 @@ bulkQueue.process(async (job) => { } // Handle retryable errors - if ( error.message.includes("HTTP Error Status: 5")) { - throw new Error("Retryable error"); // Ensures Bull retries + if ( error.message.includes("GC Notify Bulk API error: 5")) { + console.log("===============================>>>>>>>>>>>>>>>>================ Retrying job due to server error..."); + throw new Error("Retryable error"); // Ensures Bull retries } } finally { diff --git a/controllers/subscriptions.js b/controllers/subscriptions.js index fba4220..1ac9e1a 100644 --- a/controllers/subscriptions.js +++ b/controllers/subscriptions.js @@ -741,25 +741,26 @@ sendEmailViaNotify = async ( email, templateId, personalisation, notifyKey ) => } } - - - - !_bypassSubscode && notifyClient.sendEmail( templateId, email, - { - personalisation: personalisation, - reference: "x-notify_send_emails" - }) + + !_bypassSubscode && + notifyClient.sendEmail( templateId, email, + { + personalisation: personalisation, + reference: "x-notify_send_emails" + }) .catch( ( e ) => { - // Log the Notify errors + console.log((e.response && e.response.data) ); + let errorDetails = (e.response && e.response.data) ? e.response.data : null; - console.log(e.error); const currDate = new Date(), - currDateTime = currDate.getTime(), - errDetails = e.error.errors ? e.error.errors[0] : null, - statusCode = e.error.status_code, - msg = errDetails ? errDetails.message : null; - - + currDateTime = currDate.getTime(); + let statusText, errDetails, statusCode, msg; + statusText = errorDetails?.errors?.[0]?.error || errorDetails?.status_code || null; + errDetails = errorDetails?.errors?.[0] || errorDetails || e.toString(); + statusCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null; + msg = errorDetails?.errors?.[0]?.message || errorDetails?.message || errorDetails.toString(); + console.error(` --------------------> Workersendemail GC Notify Single email API error: `); + console.error(errDetails); if ( statusCode === 400 && msg.indexOf( "email_address" ) !== -1 ) { diff --git a/controllers/workerSendEmail.js b/controllers/workerSendEmail.js index 8e94861..017d87d 100644 --- a/controllers/workerSendEmail.js +++ b/controllers/workerSendEmail.js @@ -158,18 +158,21 @@ async function init() { reference: "x-notify_" + typeMailing }).catch( ( e ) => { // Log the Notify errors - // console.log( "Error in Notify" ); - // console.log( e ); - - parentPort.postMessage( { msg: "worker-Error in Notify" } ); - + console.log( "------------------> Error in Notify" ); + console.log((e.response && e.response.data) ); + let errorDetails = (e.response && e.response.data) ? e.response.data : null; + let statusText, errDetails, statusCode, msg; + // Attach status + parsed body to error object const currDate = new Date(), - currDateTime = currDate.getTime(), - errDetails = e.error.errors[0], - statusCode = e.error.status_code, - msg = errDetails.message; - - + currDateTime = currDate.getTime(); + statusText = errorDetails?.errors?.[0]?.error || errorDetails?.status_code || null; + errDetails = errorDetails?.errors?.[0] || errorDetails || e.toString(); + statusCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null; + msg = errorDetails?.errors?.[0]?.message || errorDetails?.message || e.message; + console.error(` --------------------> Workersendemail GC Notify Single email API error: `); + console.error(errDetails); + parentPort.postMessage( { msg: `worker-Error in Notify: ${statusCode} ${statusText} - ${msg}` } ); + if ( statusCode === 400 && msg.indexOf( "email_address" ) !== -1 ) {