Skip to content

Commit 8ab81dc

Browse files
committed
CENS-82 -- update error handling when interacting with GCNotify
1 parent 1f6d367 commit 8ab81dc

3 files changed

Lines changed: 68 additions & 46 deletions

File tree

controllers/bulkApiMailer.js

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,42 @@ bulkQueue.process(async (job) => {
3939
});
4040

4141
if (!response.ok) {
42-
console.error(`Bulk API failed with status: ${response.status}`);
43-
console.error(`HTTP Error Status: ${response.statusText}`)
44-
console.error(response)
42+
console.error(`Bulk API failed with status: ${response.status} - HTTP Error Status: ${response.statusText}`);
43+
let errorDetails = {};
44+
try {
45+
errorDetails = await response.json();
46+
} catch (parseErr) {
47+
console.error('Failed to parse error body:', parseErr);
48+
}
49+
50+
const err = new Error(`GC Notify Bulk API error: ${response.status} ${response.statusText}`);
51+
52+
// Attach status + parsed body to error object
53+
err.httpStatus = response.status;
54+
err.statusText = response.statusText;
55+
err.errorBody = errorDetails?.errors?.[0] || errorDetails || errorDetails.toString();
56+
err.errorCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null;
57+
err.errorMessage = errorDetails?.errors?.[0]?.message || errorDetails?.message ||response.statusText;
4558

46-
throw new Error(`HTTP Error Status: ${response.status}`);
47-
} else {
59+
console.error(` --------------------> GC Notify Bulk API error: `);
60+
console.error(err);
61+
throw err;
62+
} else {
4863
jobSuccess = true;
4964
// If request is successful, update mailing status
5065
mailingManager.mailingUpdate(jobData.mailingId, mailingState.sent, { historyState: mailingState.sending });
5166
return await response.json();
5267
}
5368

5469
} catch ( error ) {
55-
console.error("bulk q process error")
56-
console.log(error)
57-
5870
const currDate = new Date(),
5971
currDateTime = currDate.getTime();
6072

73+
const httpStatus = error.httpStatus || null;
74+
const errCode = error.errorCode || error.code || null;
75+
const errMsg = error.errorMessage || error.message;
76+
const errDetails = error.errorBody ? JSON.stringify(error.errorBody) : error.toString();
77+
6178
// Connect to MongoDB
6279
mongoInstance = await MongoClient.connect(process.env.MONGODB_URI || '', { useUnifiedTopology: true });
6380
dbConn = mongoInstance.db(process.env.MONGODB_NAME || 'subs');
@@ -66,25 +83,25 @@ bulkQueue.process(async (job) => {
6683
if ( dbConn ) {
6784
try {
6885
await dbConn.collection("notify_logs").insertOne({
69-
createdAt: currDate,
70-
jobData: job.data,
71-
err_msg: error.message,
72-
err_status: error.status,
73-
err_code: error.code,
74-
error: error.toString(),
75-
emailLength: emailLength
86+
createdAt: currDate,
87+
jobData: job.data,
88+
errorMessage: errMsg, // GCNotify message
89+
http_status: httpStatus, // HTTP status
90+
notify_Errorcode: errCode, // GC Notify error code or Node error.code
91+
errDetails: errDetails, // full JSON/error string
92+
emailLength: emailLength,
7693
});
7794
} catch ( dbError ) {
7895
console.error( "Failed to log error in notify_logs:", dbError );
79-
throw new Error ( "Bulk Queue process DB error " + dbError )
96+
throw new Error ( "Bulk Queue process DB error " + dbError.message )
8097
}
8198
}
8299

83100
//
84101
// Try to email us (only with the predefined interval)
85102
//
86103
if ( _notifyUsNotBeforeTimeLimit <= currDateTime ) {
87-
letUsKnow( "Bulk Queue error", {
104+
letUsKnow( "Bulk Queue error " + error.message + " - " + errMsg, {
88105
type: "bulk_q_process_error",
89106
currTime: currDateTime,
90107
lastTime: _notifyUsNotBeforeTimeLimit
@@ -96,8 +113,9 @@ bulkQueue.process(async (job) => {
96113
}
97114

98115
// Handle retryable errors
99-
if ( error.message.includes("HTTP Error Status: 5")) {
100-
throw new Error("Retryable error"); // Ensures Bull retries
116+
if ( error.message.includes("GC Notify Bulk API error: 5")) {
117+
console.log("===============================>>>>>>>>>>>>>>>>================ Retrying job due to server error...");
118+
throw new Error("Retryable error"); // Ensures Bull retries
101119
}
102120

103121
} finally {

controllers/subscriptions.js

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -741,25 +741,26 @@ sendEmailViaNotify = async ( email, templateId, personalisation, notifyKey ) =>
741741
}
742742

743743
}
744-
745-
746-
747-
!_bypassSubscode && notifyClient.sendEmail( templateId, email,
748-
{
749-
personalisation: personalisation,
750-
reference: "x-notify_send_emails"
751-
})
744+
745+
!_bypassSubscode &&
746+
notifyClient.sendEmail( templateId, email,
747+
{
748+
personalisation: personalisation,
749+
reference: "x-notify_send_emails"
750+
})
752751
.catch( ( e ) => {
753-
// Log the Notify errors
752+
console.log((e.response && e.response.data) );
753+
let errorDetails = (e.response && e.response.data) ? e.response.data : null;
754754

755-
console.log(e.error);
756755
const currDate = new Date(),
757-
currDateTime = currDate.getTime(),
758-
errDetails = e.error.errors ? e.error.errors[0] : null,
759-
statusCode = e.error.status_code,
760-
msg = errDetails ? errDetails.message : null;
761-
762-
756+
currDateTime = currDate.getTime();
757+
let statusText, errDetails, statusCode, msg;
758+
statusText = errorDetails?.errors?.[0]?.error || errorDetails?.status_code || null;
759+
errDetails = errorDetails?.errors?.[0] || errorDetails || e.toString();
760+
statusCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null;
761+
msg = errorDetails?.errors?.[0]?.message || errorDetails?.message || errorDetails.toString();
762+
console.error(` --------------------> Workersendemail GC Notify Single email API error: `);
763+
console.error(errDetails);
763764

764765
if ( statusCode === 400 && msg.indexOf( "email_address" ) !== -1 ) {
765766

controllers/workerSendEmail.js

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,18 +158,21 @@ async function init() {
158158
reference: "x-notify_" + typeMailing
159159
}).catch( ( e ) => {
160160
// Log the Notify errors
161-
// console.log( "Error in Notify" );
162-
// console.log( e );
163-
164-
parentPort.postMessage( { msg: "worker-Error in Notify" } );
165-
161+
console.log( "------------------> Error in Notify" );
162+
console.log((e.response && e.response.data) );
163+
let errorDetails = (e.response && e.response.data) ? e.response.data : null;
164+
let statusText, errDetails, statusCode, msg;
165+
// Attach status + parsed body to error object
166166
const currDate = new Date(),
167-
currDateTime = currDate.getTime(),
168-
errDetails = e.error.errors[0],
169-
statusCode = e.error.status_code,
170-
msg = errDetails.message;
171-
172-
167+
currDateTime = currDate.getTime();
168+
statusText = errorDetails?.errors?.[0]?.error || errorDetails?.status_code || null;
169+
errDetails = errorDetails?.errors?.[0] || errorDetails || e.toString();
170+
statusCode = errorDetails?.errors?.[0]?.code || errorDetails?.status_code || null;
171+
msg = errorDetails?.errors?.[0]?.message || errorDetails?.message || e.message;
172+
console.error(` --------------------> Workersendemail GC Notify Single email API error: `);
173+
console.error(errDetails);
174+
parentPort.postMessage( { msg: `worker-Error in Notify: ${statusCode} ${statusText} - ${msg}` } );
175+
173176

174177
if ( statusCode === 400 && msg.indexOf( "email_address" ) !== -1 ) {
175178

0 commit comments

Comments
 (0)