2009-07-08 49 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