2009-07-08 41 views
4

在我的Grails应用程序,我已经定义了以下(简体)网页流量Grails的流量异常处理

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') 
    } 
} 

如果MemberException抛出的“启动”状态,执行应该进入“handleRegMemberException”状态,但事实并非如此。我的流程定义有什么问题,或者我对这种方式的理解如何?

谢谢, 唐

回答

0

我仍然很新的Groovy和Grails,但我有一个建议。也许这个问题与Grails框架(和Groovy在这方面)处理checked和unchecked exception的方式有所不同。

如果MemberException是一个检查的异常(扩展Exception),那么封闭内部的'throw'可能会导致执行完全脱离webflow。你和我都可以在这个上做一些RTFM ...我从这里看到我书架上的Groovy书。作为一个快速回答,我会说将MemberException更改为未经检查的异常(扩展了RuntimeException)并查看是否得到相同的结果。或者,你可以换MemberException在一个RuntimeException ...

throw new RuntimeException(ex)

0

流量应该表现为您期望。 您的流量可能有问题,例如服务中存在其他一些错误,但从您的问题中可以看出实际发生的情况。你说你如何期望流动的行为,然后你说它不像你预期的那样行事,但你没有说它的行为如何。

我建议在流程中添加一些痕迹以查看实际发生的情况。

顺便说一句,有不同版本的Grails和webflows的一些已知的bug Grails中被分解1.2-M3: http://jira.codehaus.org/browse/GRAILS-5185

这里是我的流程类似,你已经设定了什么:

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() 

    } 
} 

它表现为您期望的,输出是:

-> 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