Grails Web 流异常处理
在我的 Grails 应用程序中,我定义了以下(简化的)Web 流:
def registerFlow = {
start {
action {RegistrationCommand cmd ->
try {
memberService.validateRegistrationCommandDTO(cmd)
} catch (MemberException ex) {
flow.regErrorCode = ex.errorCode
throw ex
}
}
on("success").to "survey" // The 'survey' state has been omitted
on(MemberException).to "handleRegMemberException"
on(Exception).to "handleUnexpectedException"
}
handleRegMemberException {
action {
// Implementation omitted
}
}
handleUnexpectedException {
redirect(controller:'error', action:'serverError')
}
}
如果“start”状态引发 MemberException,则执行应继续到“handleRegMemberException”状态,但事实并非如此。 我的流程定义有问题,或者我对其如何工作的理解有问题吗?
谢谢, 大学教师
In my Grails app, I have defined the following (simplified) web flow
def registerFlow = {
start {
action {RegistrationCommand cmd ->
try {
memberService.validateRegistrationCommandDTO(cmd)
} catch (MemberException ex) {
flow.regErrorCode = ex.errorCode
throw ex
}
}
on("success").to "survey" // The 'survey' state has been omitted
on(MemberException).to "handleRegMemberException"
on(Exception).to "handleUnexpectedException"
}
handleRegMemberException {
action {
// Implementation omitted
}
}
handleUnexpectedException {
redirect(controller:'error', action:'serverError')
}
}
If a MemberException is thrown by the "start" state, execution should proceed to the "handleRegMemberException" state, but it doesn't. Is there something wrong with my flow definition, or my understanding of how this should work?
Thanks,
Don
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我对 Groovy 和 Grails 还很陌生,但我有一个建议。 也许这个问题与 Grails 框架(以及 Groovy)处理检查异常和非检查异常的方式不同有关。
如果 MemberException 是一个已检查异常(扩展了 Exception),则闭包内的“抛出”可能会将执行完全带出 Web 流。 你和我都可以对此进行一些 RTFM 处理...我从这里看到我书架上的 Groovy 书。 作为一个快速回答,我想说将 MemberException 更改为未经检查的异常(扩展 RuntimeException),然后看看是否得到相同的结果。 或者,您可以将 MemberException 包装在 RuntimeException 中...
抛出新的 RuntimeException(ex)
I am still pretty new to Groovy and Grails, but I have a suggestion. Perhaps the issue has something to do with the difference in the way the Grails framework (and Groovy for that matter) handles checked vs. unchecked exceptions.
If MemberException is a checked exception (extends Exception), perhaps the 'throw' that is inside the closure behaves to bring execution the whole way out of the webflow. You and I both could do some RTFM'ing on this one... I see the Groovy book on my bookshelf from here. As a quick answer, I would say change MemberException to an unchecked exception (extends RuntimeException) and see if you get the same results. Or, you could wrap MemberException in a RuntimeException...
throw new RuntimeException(ex)
流程应该按照您的预期运行。
您的流程可能有问题,例如服务中的其他错误,但从您的问题中不清楚到底发生了什么。 你说你期望流程如何表现,然后你说它没有按照你期望的方式表现,但你没有说它实际上如何表现。
我建议在您的流程中添加一些跟踪,以查看实际发生的情况。
顺便说一句,不同版本的 grails 存在一些已知的错误,并且 webflows 在 grails 1.2-M3 中被破坏:
http://jira.codehaus.org/browse/GRAILS-5185
这是我的流程与您编写的程序类似:
它的行为符合您的预期,输出为:
The flow should behave as you expect.
Something might be wrong with your flow, for example some other error in service, but it is not clear from your question what is actually going on. You say how you expect flow to behave and then you say it doesn't behave the way you expected, but you don't say how it actually behaves.
I suggest adding some traces to your flow to see what is actually going on.
By the way, there are some known bugs with different versions of grails and webflows are broken in grails 1.2-M3:
http://jira.codehaus.org/browse/GRAILS-5185
Here is my flow similar to what you have programmed:
It behave as you expect and the output is: