3

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.

synergetic
  • 7,756
  • 8
  • 65
  • 106
  • 6
    The `except` part is catching the exception if there is one; more likely is that your code that's run in the `except` block is causing *another* exception. Check your server's error logs and find what the actual exception that's being turned into a 500 error is. – Amber Mar 31 '12 at 02:10
  • I found a way to look into server log (rhc app tail -a myapp). The problem was dir permission (beaker lock_dir was not set). I still couldn't understand why try/except didn't work, because in other cases my except part was working as expected. – synergetic Mar 31 '12 at 05:23
  • @synergetic You should avoid using bare except as it can even catch system interrupts and other system related errors and can lead to unexplained or weird errors. – codecool Mar 31 '12 at 17:38

2 Answers2

0

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

app.config['PROPAGATE_EXCEPTIONS'] = True

TheSteve0
  • 3,530
  • 1
  • 19
  • 25
0

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.

user590028
  • 11,364
  • 3
  • 40
  • 57