How to properly handle wrong urlsafe key provided?

Viewed 1074

I use the following code to get entity based on urlsafe key given:

q_key = ndb.Key(urlsafe=key)
q = q_key.get()
return q

But in case there is no such entity with given urlsafe key, it return ProtocolBufferDecodeError: Unable to merge from string on the first line, when I would expect q to be equal to None. Is there any other correct way to handle such case except of catching ProtocolBufferDecodeError exception?

2 Answers
Related