Hi Gunnar,
What I want to do is pretty simple. Let's say I have an entity with various Bean Validation constraints such as @NotNull, @Email, etc. During a REST call, if I get a ValidationConstraintException I can easily parse the ConstraintViolations into "field" and "cause" properties in a JSON error. I want exception handling to be simple and the above is very simple to do.
Now enter Phone Number. For Phone Number I need to do two things on the Entity Bean. I need to ensure that it is a valid phone number and then I need to format it into an E.164 string. Using Google's libphonenumber, that may look like this:
Code:
Contact.java:
public void setPhoneNumber( String phoneNumber ) {
this.phoneNumber = PhoneNumberUtils.format( phoneNumber, PhoneNumberType.E164 );
}
The format function will take something like "(503) 223-1414" and turn it into E.164 format "+15032231414". The format method does two things, ensure the input is valid and then format it appropriately for storage in the database. If the input is invalid it will throw a NumberFormatException.
What I would like is to use the same pattern for all validation -- email, phone number, null fields, etc. But for "phoneNumber" I have to add special handling for NumberFormatException anywhere I use it. So, I prefer NOT to add special catch blocks anywhere I use the Contact entity to catch both ConstraintViolationExceptions AND NumberFormatExceptions.
Does this help explain the issue?
R.