添加 "retry" 导致我丢失了回溯
Adding "retry" caused me to lose traceback
目前我有一个这样定义的脚本:
import traceback
import logging
import time
logging.basicConfig(level=loggings.DEBUG)
logger=logging.getLogger(__name__)
if __name__ == '__main__':
try:
main_code()
except Exception as e:
logger.debug(traceback.format_exc())
error = traceback.format_exc()
with smtplib.SMTP("mailhub.global.company.com") as mailServer:
msg = MIMEMultipart('related')
msg['Subject'] = Header(u'Test', 'utf-8')
msg['X-Priority'] = '2'
body = "Delivery Failure, the following exception occurred:\n\n" + str(error)
msg.attach(MIMEText(body, 'plain'))
mailServer.sendmail("email@gmail.com", ["email@gmail.com"], msg.as_string())
mailServer.quit()
print("Exception occurred- delivery failure email sent")
如果有错误,我会收到一封带有回溯的电子邮件。
我试图在代码中添加一个 "retry" 以防失败,这样它会尝试 5 次,中间间隔 5 秒,如下所示:
if __name__ == '__main__':
for attempt in range(5):
try:
main_code()
except Exception as e:
print("Error occurred, retrying in 5 seconds!")
time.sleep(5)
continue
else:
break
else:
logger.debug(traceback.format_exc())
error = traceback.format_exc()
with smtplib.SMTP("mailhub.global.company.com") as mailServer:
msg = MIMEMultipart('related')
msg['Subject'] = Header(u'Test', 'utf-8')
msg['X-Priority'] = '2'
body = "Delivery Failure, the following exception occurred:\n\n" + str(error)
msg.attach(MIMEText(body, 'plain'))
mailServer.sendmail("email@gmail.com", ["email@gmail.com"], msg.as_string())
mailServer.quit()
print("Exception occurred- delivery failure email sent")
然而,出于某种原因,当我现在收到一封发送失败的电子邮件时,它只是说:Delivery Failure, the following exception occurred: NoneType: None
知道如何将回溯回溯到已发送电子邮件中的错误吗?
当您离开 except
块时,异常是 "cleaned up"(已销毁),因此当您到达最终 traceback
模块中的函数时无法看到它=12=]块。您可能想将 error = traceback.format_exc()
移动到 except
块中(并将 error
传递给 logger.debug()
)。
目前我有一个这样定义的脚本:
import traceback
import logging
import time
logging.basicConfig(level=loggings.DEBUG)
logger=logging.getLogger(__name__)
if __name__ == '__main__':
try:
main_code()
except Exception as e:
logger.debug(traceback.format_exc())
error = traceback.format_exc()
with smtplib.SMTP("mailhub.global.company.com") as mailServer:
msg = MIMEMultipart('related')
msg['Subject'] = Header(u'Test', 'utf-8')
msg['X-Priority'] = '2'
body = "Delivery Failure, the following exception occurred:\n\n" + str(error)
msg.attach(MIMEText(body, 'plain'))
mailServer.sendmail("email@gmail.com", ["email@gmail.com"], msg.as_string())
mailServer.quit()
print("Exception occurred- delivery failure email sent")
如果有错误,我会收到一封带有回溯的电子邮件。
我试图在代码中添加一个 "retry" 以防失败,这样它会尝试 5 次,中间间隔 5 秒,如下所示:
if __name__ == '__main__':
for attempt in range(5):
try:
main_code()
except Exception as e:
print("Error occurred, retrying in 5 seconds!")
time.sleep(5)
continue
else:
break
else:
logger.debug(traceback.format_exc())
error = traceback.format_exc()
with smtplib.SMTP("mailhub.global.company.com") as mailServer:
msg = MIMEMultipart('related')
msg['Subject'] = Header(u'Test', 'utf-8')
msg['X-Priority'] = '2'
body = "Delivery Failure, the following exception occurred:\n\n" + str(error)
msg.attach(MIMEText(body, 'plain'))
mailServer.sendmail("email@gmail.com", ["email@gmail.com"], msg.as_string())
mailServer.quit()
print("Exception occurred- delivery failure email sent")
然而,出于某种原因,当我现在收到一封发送失败的电子邮件时,它只是说:Delivery Failure, the following exception occurred: NoneType: None
知道如何将回溯回溯到已发送电子邮件中的错误吗?
当您离开 except
块时,异常是 "cleaned up"(已销毁),因此当您到达最终 traceback
模块中的函数时无法看到它=12=]块。您可能想将 error = traceback.format_exc()
移动到 except
块中(并将 error
传递给 logger.debug()
)。