10

I have a function which catches all exceptions, and I want to be able to get the traceback as a string within this function.

So far this is not working:

def handle_errors(error_type, error_message, error_traceback):
    """catch errors"""
    import traceback
    error = {}
    error['type'] = error_type.__name__
    error['message'] = str(error_message)
    error['file'] = os.path.split(error_traceback.tb_frame.f_code.co_filename)[1]
    error['line'] = error_traceback.tb_lineno
    error['traceback'] = repr(traceback.print_tb(error_traceback))
    ### finalise error handling and exit ###

sys.excepthook = handle_errors

It's the error['traceback'] line which is wrong. Do i even need to use the traceback module?

As per this other vaguely similar question, I have tried:

error['traceback'] = repr(error_traceback.print_exc())

...but this gives an error:

Error in sys.excepthook:
Traceback (most recent call last):
  File "xxxxxxxxxxx", line 54, in handle_errors
    error['traceback'] = repr(error_traceback.print_exc())
AttributeError: 'traceback' object has no attribute 'print_exc'
Community
  • 1
  • 1
mulllhausen
  • 4,225
  • 7
  • 49
  • 71
  • possible duplicate of [Get full traceback](http://stackoverflow.com/questions/13210436/get-full-traceback) – Paolo Casciello Nov 27 '13 at 11:43
  • that question is not applicable. i will update my question explaining why – mulllhausen Nov 27 '13 at 11:47
  • Hello. See my answer please. Why did you wrote that your question isn't applicable ? – eyquem Dec 12 '13 at 15:08
  • @eyquem because [that other question](http://stackoverflow.com/questions/13210436/get-full-traceback) is not about capturing a traceback within an excepthook function. the excepthook function provides a traceback frame to query (`error_traceback`) but that other question does not address how to use this frame. – mulllhausen Dec 13 '13 at 13:07
  • I don't understand what you say because of _"that other question"_ ? Which **other** question ?? – eyquem Dec 13 '13 at 14:19
  • Besides, I remark something I didn't until now: in the definition of ``handle_errors``, the reference _error['traceback']_ is assigned to repr() of ``traceback.print_tb(error_traceback))`` while the account of the exception says it is repr() of ``error_traceback.print_exc())`` – eyquem Dec 13 '13 at 14:23
  • to see the other question i am talking about, please click on the link-words 'that other question' in my previous comment. its the one which you have put `This question may already have an answer here:` at the top of my original post – mulllhausen Dec 14 '13 at 00:01

2 Answers2

19

Use traceback.format_tb() instead of print_tb() to get the formatted stack trace (as a list of lines):

error['traceback'] = ''.join(traceback.format_tb(error_traceback))

print_tb() directly prints the traceback, that's why you get None as a result (that's the default for any Python function that doesn't return anything explicitely).

Lukas Graf
  • 30,317
  • 8
  • 77
  • 92
0

traceback.format_exc([limit])
This is like print_exc(limit) but returns a string instead of printing to a file.

New in version 2.4.

error['traceback'] = traceback.format_exc(error_traceback)
eyquem
  • 26,771
  • 7
  • 38
  • 46
  • 1
    This does not work due to reasons pointed out by Lukas G. in the accepted answer that was posted earlier than yours. – andyn Oct 27 '16 at 08:56