Translating PropertyEditor exceptions to messages
My PropertyEditor class resolves "id strings" (essentially primary keys) to domain objects of some specific class.
A manager class (could be a Dao or higher level service) is injected for this purpose.
These are three possible failure points in the setAsText method execution:
1) The string representation being converted is not of the correct format.(NumberFormatException)
2) No object can be found for the supplied id.(ObjectNotFoundException thrown by manager)
3) The object exists, but is not authorized for use by the current user.(SecurityException thrown by manager)
I can't find much information in the reference docs or the sample apps on how to handle exceptions from custom PropertyEditors.
public class FoobarPropertyEditor extends PropertyEditorSupport
private FoobarManager manager;
public FoobarPropertyEditor(FoobarManager manager)
this.manager = manager;
public String getAsText()
Foobar foobar = (Foobar)getValue();
if(foobar == null) return "";
else return String.valueOf(foobar.getId());
public void setAsText(String text) throws IllegalArgumentException
int id = Integer.parseInt(text);
foobar = manager.find(id);
throw new IllegalArgumentException("Invalid value", nfe);
throw new IllegalArgumentException("Foobar not found", enfe)
throw new IllegalArgumentException("Access to foobar denied", se)
How do I customize the translation from various IllegalArgumentExceptions to user-friendly messages?
Some piece of code should examine the IllegalArgumentException, retrieve the nested cause, and work with that. But how do I inject such logic into the DataBinder?
Re: user-friendly messages
The problem with above work-around is that the nondescriptive typeMismatch.user.age could be caused by different domain exceptions.
Originally Posted by njayalath
I want to be able to distinguish between
-object not found
-some domain specific logic preventing the user from obtaining the entity instance
-perhaps an actual type mismatch (e.g. user filled in nonsense input)
By the time my message translator gets a chance, the relevant information has already been dropped by the databinder.
To summarize, the functionality I'm after is a strategy interface to let the binding infrastructure translate IllegalArgumentExceptions (actually the nested cause) thrown by a PropertyEditor to custom message codes.