简体   繁体   中英

python try except does not capture all errors

In my flask web application I use beaker library for session handling. In the following code, for some unknown reason, production server raises exception, but my local pc does just fine.

import sys

...

try:
    beaker_session = request.environ['beaker.session']
    beaker_session['user_id'] = user.id 
    beaker_session.save()
except:
    flash(sys.exc_info()[0])
    return render_template('main/login.html')

Local computer saves the session just as expected, without any exception. Production server (RedHat OpenShift) raises an error exactly on "beaker_session.save()" line. But, instead of showing my login page with flash message, Internal Server Error 500 is thrown. I checked beaker backend url (mysql db) and there's no problem, because it works in other parts of code, where I persist newly registered users. So, my question is 1) why except part doesn't work? 2) why beaker cannot save the session. Thank you.

By default Flask swallows exceptions, be sure to add this line to your application near the top:

app.config['PROPAGATE_EXCEPTIONS'] = True

I'm not familiar with the beaker library, but if it's a SWIG wrapped library, and the exception ocurrs within the C++ code, it's possible the designer neglected to map the exception to an appropriate python exception. If this is the case, then Python does not get a shot at the exception -- and even try/except will miss it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM