开发者

Exception Handling in google app engine

开发者 https://www.devze.com 2022-12-25 03:23 出处:网络
i am raising exception using if UserId == \'\' and Password == \'\': raise Exception.MyException , \"wrong userId or password\"

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

clas开发者_高级运维s MyException(Exception):
    def __init__(self,msg):
        Exception.__init__(self,msg)


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.)


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

0

精彩评论

暂无评论...
验证码 换一张
取 消