We have a usecase to send a mail to end users regarding few information.
The current way which we are doing is using a python script, which inturn is called from a Express JS script:
Current code:
request.post(MailURL)
.set('Content-Type', 'application/json')
.send({
emailId: <email_ids>,
ccId: <email_ids>,
emailSubject: `TEST SUBJECT`,
emailBody: `TEST BODY`
})
.end((err, res) => {
if (err) {
logger.error(logMessage, 'Error when sending a mail', err);
return res.status(500).json({
message: 'Error when sending a mail',
error: err,
});
}
logger.info(logMessage, 'Successfully sent mail');
});
Here Mail URL is the local Express container URL.
Python Code:
for send_attempt in range(1, max_retry+1):
try:
parsed_args = parseArgs()
mailer = Mailer(parsed_args.recipients, parsed_args.subject, parsed_args.cc)
mailer.attach_html_body()
mailer.send_message()
mailer.remove_file()
break
except socket.error as socket_error:
logging.warning("Attempt %d to send email failed...", send_attempt)
logging.error("Socket Error: %s", os.strerror(socket_error.errno))
if send_attempt >= max_retry:
logging.error("Could not send email after %d attempts.", max_retry)
We are trying to catch the exception, but still the Error is coming, due to which the Express container is going down with below Error:
{ Error: socket hang up\n at createHangUpError (_http_client.js:331:15)\n at Socket.socketOnEnd (_http_client.js:423:23)\n at emitNone (events.js:111:20)\n at Socket.emit (events.js:208:7)\n at endReadableNT (_stream_readable.js:1055:12)\n at _combinedTickCallback (internal/process/next_tick.js:138:11)\n at process._tickCallback (internal/process/next_tick.js:180:9) code: 'ECONNRESET', response: undefined }
return res.status(500).json({
^
TypeError: Cannot read property 'status' of undefined
Could someone help what is going wrong here, or how we can prevent container from going down?