@@ -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 {
0 commit comments