gpt4 book ai didi

grails - 如何知道验证错误的原因

转载 作者:行者123 更新时间:2023-12-02 14:05:13 25 4
gpt4 key购买 nike

如果保存由于某种原因失败,则以下代码将引发grails.validation.ValidationException。但是结果是一个通用错误。如何知道错误的实际原因,以便将其报告给用户?

 def addChild(cName,Parent theParent) {
println "add child: ${cName}"
def theChild = new Child(name:cName,parent:theParent)
theChild.save(failOnError:true)
return theChild
}

这是返回的堆栈跟踪。我碰巧知道这是由于违反了唯一的约束所导致的,因为我是故意造成的,但是在痕迹中没有任何东西可以表明这是原因与其他约束的违反。
org.codehaus.groovy.runtime.InvokerInvocationException: grails.validation.ValidationException: Validation Error(s) Occurred During Save

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)

at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)

at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)

at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)

at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)

at java.lang.Thread.run(Thread.java:619)

Caused by: grails.validation.ValidationException: Validation Error(s) Occurred During Save

at AddRecordsService.addChild(AddRecordsService.groovy:30)

at AddRecordsService$addChild.callCurrent(Unknown Source)

at AddRecordsService.addAll(AddRecordsService.groovy:11)

at AddRecordsService$$FastClassByCGLIB$$e47d68f4.invoke(<generated>)

at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:149)

at AddRecordsService$$EnhancerByCGLIB$$cdfdcc61.addAll(<generated>)

at AddRecordsService$addAll.call(Unknown Source)

at AddrecordController$_closure2.doCall(AddrecordController.groovy:14)

at AddrecordController$_closure2.doCall(AddrecordController.groovy)

... 32 more

更新

好吧,看来至少到目前为止,使事务回滚并找出导致错误的唯一方法是检查保存是否失败,获取failedobject.errors并抛出RuntimeException。但是现在您如何将错误传递回调用 Controller ?以下无效。
   def addChild(cName,Parent theParent) {
println "add child: ${cName}"
def theChild = new Child(name:cName,parent:theParent)

//theChild.save(failOnError:true)
//theChild.save()


if(!theChild.save()){
println theChild.errors
throw new RuntimeException(theChild.errors)
//throw new RuntimeException('unable to save child')
}


return theChild
}

最佳答案

旧问题,但是否有人偶然发现:在服务中,Grails(2.1)习惯用法是调用save(),如果save失败,则抛出ValidationException。

 if (!theChild.save()) {
throw new ValidationException(theChild.errors)
}

您通常不调用 save(failOnError: true)。相反,在您的 Controller 中,您捕获了ValidationException:
try {
service.addChild(child)
} catch (ValidationException e) {
errors.allErrors.each {
// do something with each error code
}
}

如果您在 Controller 中完成所有工作,则具有相同的想法。在这种情况下,您当然不必抛出异常然后捕获异常,您可以当场处理 child.errors

关于grails - 如何知道验证错误的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1640666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com