Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to just print a simple traceback of the exception passed to the errorhandler function in python flask

Tags:

python

flask

I have some code written in python flask, where I have a function as follows:

@app.errorhandler(500)
def internal_error(exception):
    print "500 error caught"

But this message is not sufficient enough to provide me with enough information. I just want to print the traceback for the exception that is passed to errorhandler. Is there any way to do this simple thing?

like image 286
Sanjiban Bairagya Avatar asked Jan 07 '23 01:01

Sanjiban Bairagya


2 Answers

Assuming that the error handler is called from within a context when the exception and traceback are still available from sys, you should be able to use traceback.format_exc.

import traceback

@app.errorhandler(500)
def internal_error(exception):
    print "500 error caught"
    print traceback.format_exc()
like image 158
mgilson Avatar answered Jan 24 '23 14:01

mgilson


I think you can solve it like this:

import sys
import traceback

@app.errorhandler(500)
def internal_error(exception):
    print("500 error caught")
    etype, value, tb = sys.exc_info()
    print(traceback.print_exception(etype, value, tb))

You can print the traceback information

like image 21
GoingMyWay Avatar answered Jan 24 '23 15:01

GoingMyWay