Monica Leko
3/3/2008 3:33:00 PM
Suppose you have some HTML forms which you would like to validate.
Every field can have different errors. For example, this are the
forms:
username
password
etc
And you want to validate them with some class. Is this good pattern:
class Foo(object):
def validateUsername(username):
if username isn't correct:
raise ValidationError()
def validatePassword(password):
if password isn't correct:
raise ValidationError()
code:
try:
usernameError = validateUsername()
except ValidationError:
usernameError = error from exception
try:
passwordError = validatePassword()
except ValidationError:
passwordError = error from exception
So, if there wasn't any errors, usernameError and passwordError both
contains None, and there was error, both contains some string? Should
I use exception or just return None or some string without
exception?