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)
Code:
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
{
try
{
int id = Integer.parseInt(text);
foobar = manager.find(id);
setValue(foobar);
}
catch(NumberFormatException nfe)
{
throw new IllegalArgumentException("Invalid value", nfe);
}
catch(EntityNotFoundException enfe)
{
throw new IllegalArgumentException("Foobar not found", enfe)
}
catch(SecurityException se)
{
throw new IllegalArgumentException("Access to foobar denied", se)
}
}
}
I can't find much information in the reference docs or the sample apps on how to handle exceptions from custom PropertyEditors.
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
Quote:
Originally Posted by njayalath
In order to customize the translation from various IllegalArgumentExceptions to user-friendly messages you could take a look at "DefaultMessageCodesResolver"
E.g. in case of code "typeMismatch", object name "user", field "age":
try "typeMismatch.user.age"
The problem with above work-around is that the nondescriptive typeMismatch.user.age could be caused by different domain exceptions.
I want to be able to distinguish between
-object not found
-security failure
-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.