I wish to handle NoneType errors from the Google App Engine ndb Datastore.
Here is my code:
def post(self):
# get the email and password from the login form
global u_qry
email = self.request.get("email")
password = self.request.get("password")
try:
u_qry = user_db.User.get_by_email(email)
except BaseException, e:
self.response.out.write(e, u_qry)
else:
self.response.out.write("User Recognised")
self.response.out.write(u_qry)
When the user does not exist, the except clause is not catching any error.
Output:
User Recognised None
why is the except clause not catching this error? What exception can be used to catch this error?
I realise I can use if/else statements on this, but would like to learn how to use the try/except clause in GAE.
Thanks.
While you're not showing the way the User.get_by_email()
function is implemented I suspect it is simply based on a query. And returning an empty result from a query is a perfectly valid situation - no existing datastore entity matches the query. It's not an exception.
If you intend to consider such case an exception you have to raise one yourself inside User.get_by_email()
and in that case you'd be able to catch it in your post()
function.
Alternatively you can directly check u_qry
for a None
value.