Domanda

I am needing to return conditional content back to a jQuery getJSON() function from a Python script.

The Python script checks whether a user is logged in with:

aaa.require()

The require function has a built in bottle.redirect on authorisation failure, but that just communicates directly to the Python script, whereas I need to pass that message back to jQuery.

There is an exception class called AuthException that is raised on authorisation failure.

Can I do something like:

aaa.require()
if AuthException:
    # send something back to jQuery

In other words, is it possible to make a conditional statement based on whether an exception is raised during a function call?

È stato utile?

Soluzione

If you are using Cork, catch the AuthException exception raised only if you didn't set the fail_redirect keyword; you can then just catch that exception:

from cork.cork import AuthException

try:
    aaa.require()
except AuthException as ae:
    # send back a JSON response
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top