Meaningful error messages?
Hi Arno,
I definitely understand what you mean with regards to the problems associated with maintaining the validation rules in the database and Java code. But my question is this: even if you do catch the DataIntegrityViolationException, how would you provide an informative error message to the user? What if a single entity, such as a User that is stored in the USER table, has multiple columns with unique constraints (such as USERNAME and EMAIL_ADDRESS)? How would you inform the user which field(s) he entered already exists in the database so that he can fix the problem?
Maybe there is a way to inspect the DataIntegrityViolationException to determine the offending field(s), but can you provide a corresponding user-friendly error message to explain to the user?
For this reason, it makes sense to me to enforce the rules during pre-insertion validation. As Colin mentioned, there is still a window in which data that passes this initial validation could still be rejected by the database. Maybe in those cases, a generic error could be reported to the user. But subsequent attempts would be caught during pre-insertion validation, and a meaningful error message could be provided.
-Arthur Loder