How to handle errors that occur in @BindUsing annotation closure

て烟熏妆下的殇ゞ 提交于 2020-01-04 01:36:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!