python - Catching the exception tracebacks in a loop and then raising the error(s) at the end of the script -


i'm trying catch exception error(s) , @ end of script raise/show tracebacks...

i have main script calls subscripts example:

    errors = open('misc/error(s).txt', 'a')      try:         execfile("subscripts/test1.py", {})     except exception:         ## spread on 2 calls of errors.write readability in code...         errors.write(strftime('%d/%m/%y %h:%m:%s') + "\n")         errors.write(traceback.format_exc() + '\n\n')      try:         execfile("subscripts/test2.py", {})     except exception:         ## spread on 2 calls of errors.write readability in code...         errors.write(strftime('%d/%m/%y %h:%m:%s') + "\n")         errors.write(traceback.format_exc() + '\n\n')      errors.close() 

this script uses traceback module retrieve error script...

in next example current script sort of looks like:

for x in y:     if "example" in x:         tweet in tweetlist:             # try             try:                 twitter.update_status(status=tweet)                 # other stuff here if suceeeds like...                 print "oo example worked"             # damn failed, grab whole traceback?             except exception reason:                 failedtweet = true  # other stuff here like... print "i did other stuff"  if failedtweet:     print reason # printing reason because don't know how throw exception error (full error) 

basically there big loop potentially go wrong around line: twitter.update_status(status=tweet) , if want catch traceback error(s) could more 1 because it's in loop , when script finishes want send traceback errors main script can write them error file.

example of error writing file first bit of code:

# 17/08/2014 12:30:00 # traceback (most recent call last): #   file "c:\main.py", line 117, in execute_subscripts #     execfile("subscripts/test1.py", {}) #   file "subscripts/test1.py", line 440, in <module> #     twitter.update_status(status=string) #   file "c:\python27\lib\site-packages\twython\endpoints.py", line 90, in update_status #     return self.post('statuses/update', params=params) #   file "c:\python27\lib\site-packages\twython\api.py", line 234, in post #     return self.request(endpoint, 'post', params=params, version=version) #   file "c:\python27\lib\site-packages\twython\api.py", line 224, in request #     content = self._request(url, method=method, params=params, api_call=url) #   file "c:\python27\lib\site-packages\twython\api.py", line 194, in _request #     retry_after=response.headers.get('retry-after')) # twythonerror: twitter api returned 403 (forbidden), request looks might automated. protect our users spam , other malicious activity, can't complete action right now. please try again later. 

how achieve this, it's kind of hard explain if doesn't make sense please ask.

just save traceback data in list, , print contents of list after loop complete.

import traceback  reasons = [] x in y:     if "example" in x:         tweet in tweetlist:             # try             try:                 twitter.update_status(status=tweet)                 # other stuff here if suceeeds like...                 print "oo example worked"             # damn failed, grab whole traceback?             except exception:                 reasons.append(traceback.format_exc())  # other stuff here like... print "i did other stuff"  reason in reasons:     print reason  # if want raise single exception shows traceback # each exception, can this: class chainedexception(exception):     def __init__(self, msg):         msg = "the following exceptions occurred:\n\n{}".format(msg)  if reasons:     raise chainedexception('\n'.join(reasons)) 

example usage of chainedexception:

reasons = [] in range(5):     try:         raise exception("blah {}".format(i))     except exception:         reasons.append(traceback.format_exc())  if reasons:     raise chainedexception("\n".join(reasons)) 

output:

traceback (most recent call last):   file "ok.py", line 17, in <module>     raise chainedexception("\n".join(reasons)) __main__.chainedexception: following exceptions occurred:  traceback (most recent call last):   file "ok.py", line 12, in <module>     raise exception("blah {}".format(i)) exception: blah 0  traceback (most recent call last):   file "ok.py", line 12, in <module>     raise exception("blah {}".format(i)) exception: blah 1  traceback (most recent call last):   file "ok.py", line 12, in <module>     raise exception("blah {}".format(i)) exception: blah 2  traceback (most recent call last):   file "ok.py", line 12, in <module>     raise exception("blah {}".format(i)) exception: blah 3  traceback (most recent call last):   file "ok.py", line 12, in <module>     raise exception("blah {}".format(i)) exception: blah 4 

edit:

if care raising single exception out of entire list of exceptions, can this:

import traceback  reason = none x in y:     if "example" in x:         tweet in tweetlist:             # try             try:                 twitter.update_status(status=tweet)                 # other stuff here if suceeeds like...                 print "oo example worked"             # damn failed, grab whole traceback?             except exception:                 reason = sys.exc_info() # we're not putting in list because care one.  # other stuff here like... print "i did other stuff"  if reason:     raise reason[0], reason[1], reason[2] 

note works in python 2.x. if using python 3.x, need use this:

if reason:     raise reason[1].with_traceback(reason[2]) 

Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -