Grails web stream exception handling

In my Grails application, I defined the following (simplified) web stream

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 event is thrown by the "start" state, execution should go to the "handleRegMemberException" state, but this is not the case. Is there something wrong with my definition of flow or my understanding of how this should work?

Thanks Don

+4
source share
2 answers

I'm still pretty new to Groovy and Grails, but I have a suggestion. Perhaps the problem has something to do with the difference in how the Grails framework (and Groovy, for that matter) handles checked or thrown exceptions.

If MemberException is a thrown exception (extends Exception), it is possible that a β€œthrow” that is inside the closure will cause the entire exit from the web stream to complete. Both of you and I could do some RTFM'ing on this ... I see a Groovy book on my bookshelf from here. As a quick answer, I would say that I changed MemberException to an unchecked exception (extends RuntimeException) and see if you get the same results. Or you can wrap MemberException in a RuntimeException ...

throw new RuntimeException(ex)

0
source

The flow should behave as you expect. Something may be wrong with your flow, for example, with a different service error, but it is not clear from your question what is really happening. You say how you expect the flow to behave, and then you say that it does not behave as you expected, but you do not say how it behaves.

I suggest adding some traces to the stream to see what really happens.

By the way, there are some known errors with different versions of grails, and webflows are broken in grails 1.2-M3: http://jira.codehaus.org/browse/GRAILS-5185

Here is my thread, similar to what you programmed:

 class SomeController { def index = { redirect(action:'someProcess') } def someProcessFlow = { start{ action{ dosome -> println "-> inside start action closure" try{ println "-> throwing IllegalArgumentException" throw new IllegalArgumentException() }catch(IllegalArgumentException ex){ println "-> inside catch" throw ex } throw new Exception() "success" } on("success").to "helloPage" on(IllegalArgumentException).to "illegal" on(Exception).to "handleException" } illegal{ action{ println "-> illegal handled" "success" } on("success").to "helloPage" } handleException{ action{ println "-> generic exception handled" "success" } on("success").to "helloPage" } helloPage() } } 

It behaves as you expect, and the conclusion is:

 -> inside start action closure -> throwing IllegalArgumentException -> inside catch 2009-11-03 11:55:00,364 [http-8080-1] ERROR builder.ClosureInvokingAction - Exception occured invoking flow action: null java.lang.IllegalArgumentException at SomeController$_closure2_closure3_closure6.doCall(SomeController:18) at java.lang.Thread.run(Thread.java:619) -> illegal handled 
0
source

All Articles