I’m relatively new to SQLAlchemy.
I’ve a very simple model with a custom @validates function using the sqlalchemy decorator:
class User(db.Model):
...
email = db.Column(db.String(255)) # don't worry about uniqueness for now
@validates('email')
def validate_email(self, key, address):
assert '@' in address # of course a regex is better, I know
return address
and then later:
try:
user = User('fidel#cuba.cu')
db.session.add(user)
db.session.commit()
except Exception as e:
print e.message # returns an empty string ''
what I’d like to do:
...
except Exception as e:
# json_error is a custom function of my own
return json_error(e.message, 400)
So the question is:
How do I attach a custom error message to a custom validation rule in SQLAlchemy?
Maybe I need to create a custom Exception with the message in it?
Assertions can take a second argument – the message to use if the assertion fails: