Hi Namita, I find your question a little bit unclear.
I try to answer the so that they understand where exactly the error is getting part. To achieve this, you can think of sending the whole traceback using the traceback
module.
import traceback
import sys
import itertools
def someFileRunningCode(err):
if not err:
pass
else:
raise Exception('Error from someFileRunningCode')
def codeForCalculation(err):
if not err:
pass
else:
raise Exception('Error from codeForCalculation')
def sendEmail(user, email):
print('To: {usr}\n{text}\n'.format(usr = user, text = email))
def calculateFleet(err_fr, err_cc):
someFileRunningCode(err_fr)
try:
codeForCalculation(err_cc)
except Exception as E:
raise Exception(E) from E
if __name__ == '__main__':
user = 'dummy.user@foo.com' # mock user email
for err in itertools.product((True, False), repeat = 2):
msg = ['OK' if not e else 'ERROR' for e in err]
print('- file running: {}\n- calculation: {}'.format(msg[0], msg[1]))
try:
calculateFleet(err[0], err[1])
email = 'Everything ok, here are the results.'
except Exception as E:
#
email = ('Something went wrong while processing your data,'
+ 'here is the traceback:\n {}'.format(traceback.format_exc()))
finally:
sendEmail(user, email)
In the above example, the main loop explores all the possible combinations of failures for the case at hand and the sendEmail
function allows you to have a print preview of what the user will receive in the email body.
If this does not fulfill your needs, please provide a clearer example of the expected output (e.g. error in someFileRunningCode
--> message to be displayed...).