问题
I have a property with custom data binding like this:
class User {
// Custom data binding for the image
@BindUsing({ obj, source ->
def imageFile = source['image']
if (imageFile && imageFile.size > 0) {
Document image = obj.image ?: new Document()
image.setFile(imageFile)
if(!image.save()) {
throw new Exception('Non localizable message')
}
return image
}
})
Document image
...
}
If an exception is thrown (like in my example) it is converted to a ValidationException
with always the same generic error codes for typeMismatch
:
[com.example.security.User.image.typeMismatch.error,com.example.security.User.image.typeMismatch,user.image.typeMismatch.error,user.image.typeMismatch,typeMismatch.com.example.security.User.image,typeMismatch.image,typeMismatch.com.example.common.Document,typeMismatch]
The defaultMessage
of the ValidationException
is the message of the exception that was thrown in @BindUsing
. So to get localized messages one would have to inject messageSource
somehow into @BindUsing
and localize the message of the exception that is thrown.
What is the correct way to return an error or an error code from @BindUsing
?
来源:https://stackoverflow.com/questions/29253601/how-to-handle-errors-that-occur-in-bindusing-annotation-closure