Rahul99
Rahul99

Reputation: 85

Exception Handling in google app engine

i am raising exception using

if UserId == '' and Password == '':
    raise Exception.MyException , "wrong userId or password" 

but i want print the error message on same page

class MyException(Exception):
    def __init__(self,msg):
        Exception.__init__(self,msg)

Upvotes: 1

Views: 1162

Answers (2)

jholster
jholster

Reputation: 5136

You are not using the Users API? Assuming you are handling a POST request, how about this:

class LoginError(Exception):
    CODES = { 'mismatch': 'Wrong credentials', 'disabled': 'Account disabled' }
    ...

try:
    // your authentication code
    raise LoginError('mismatch')
    ...
    raise LoginError('disabled')
except LoginError as e:
    self.redirect(your_login_url + '?err=' + e)

# In login page you must not print arbitrary GET parameter directly
err_reason = LoginError.CODES[self.request.get('err')]`

(Login request should be using POST method because it changes the server's state, and it's good habit to redirect after a POST, thus a redirect.)

Upvotes: 1

Ilian Iliev
Ilian Iliev

Reputation: 3236

Why raising an exception instead of just stop function execution and redirect to new page using return statement

Upvotes: 0

Related Questions