Sanjiban Bairagya
Sanjiban Bairagya

Reputation: 744

How to just print a simple traceback of the exception passed to the errorhandler function in 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?

Upvotes: 7

Views: 11306

Answers (2)

GoingMyWay
GoingMyWay

Reputation: 17468

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

Upvotes: 1

mgilson
mgilson

Reputation: 309929

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()

Upvotes: 10

Related Questions