grails - Service call back to a Controller? -


grails makes easy controller call service , controller forward request onto controller.

so suppose have service method such

list<string>  updatenames() {    ... } 

you can call controller.

i wondering, if have edge case realise there validation problem in service method. don't want throw exception controller, because not exceptional case. can't return error message service controller called because mean have use wrapper object instead of nice list

is there anyway these cases, can service server side forward onto controller return error response user?

thanks.

grails have structure validation in beans, called errors (comming form spring). example, if have service upload files, attach validation errors in bean:

class uploadservice {   void doupload(multipartfile file, mydomainclass domainclassinstance) {     if(validationsfail) {       domainclassinstance.errors.rejectvalue("myuploadfield","my.i18n.code")     }   } } 

if it's not domain class, can consider using command object since they're validateable too.

in controller, it's metter of checking if instance has errors:

def upload() {   mydomainclass instance = ...   uploadservice.doupload(request.getfile('file'), instance)   if(!instance.haserrors()) {     //save , go on...   } } 

another option work exceptions @joshua moore answered. remember extend runtimeexception. if don't, transaction not rolledback automatically.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -