Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions controllers/bulkApiMailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,42 @@ 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 });
return await response.json();
}

} 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');
Expand All @@ -66,25 +83,25 @@ 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 )
}
}

//
// 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
Expand All @@ -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 {
Expand Down
33 changes: 17 additions & 16 deletions controllers/subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down
25 changes: 14 additions & 11 deletions controllers/workerSendEmail.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {

Expand Down