2017-07-26 48 views
0
  1. 爲什麼我在控制器中拋出和異常是錯誤的?
  2. 爲什麼在控制器中拋出異常暫停執行,但在服務中拋出異常呢?
  3. 如果可能的話,我錯過了什麼來停止執行,並且在從控制器中拋出時只執行handleCustomException方法?

我在一個簡單的項目中重新創建了這個問題,看看它是不是我曾經意外完成的事情,但它似乎是默認的grails行爲。Grails - 爲什麼在控制器中拋出的異常暫停執行,但在服務中拋出異常呢

我有一個包含單個控制器:

package simpletestproject 

import simpleTestProject.exceptions.CustomException 

class ExceptionTestController { 

    SimpleService simpleService 

    def index() { 
     println("index called") 
     someMethod() 
     println("someMethod has been called") 
    } 

    def viaService() { 
     println("viaService called") 
     simpleService.serviceMethod() 
     println("simpleService.someMethod has been called") 
    } 

    def someMethod() throws CustomException{ 
     println("foo... someMethod") 
     throw new CustomException("some Response with an errocode", "You have seen an exception from the CONTROLLER") 
     println("this should not be seen") 
    } 

    def handleCustomException(final CustomException exception){ 
     println("EXCEPTION CAUGHT - ${ exception.getErroneousResponse() } - ${ exception.getMessage() }") 
     render("Exception Handled") 
    } 
} 

和含有一個單一的服務:

package simpletestproject 

import grails.transaction.Transactional 
import simpleTestProject.exceptions.CustomException 

@Transactional 
class SimpleService { 

    def serviceMethod() { 
     println("serviceMethod") 
     throw new CustomException("some Response with an errocode", "You have seen an exception from the SERVICE") 
     println("serviceMethod - this should not be seen") 
    } 
} 

如果我導航到http://localhost:8080/simpleTestProject/ExceptionTest我看到以下印刷:

index called 
foo... someMethod 
EXCEPTION CAUGHT - some Response with an errocode - You have seen an exception from the CONTROLLER 
someMethod has been called 

如果我導航到http://localhost:8080/simpleTestProject/ExceptionTest/viaService我看到以下印:

viaService called 
serviceMethod 
EXCEPTION CAUGHT - some Response with an errocode - You have seen an exception from the SERVICE 

僅供參考 - 我的CustomException如下:

package simpleTestProject.exceptions 

class CustomException extends RuntimeException { 

    private Object erroneousResponse 

    public CustomException(Object erroneousResponse, String message) { 
     super(message) 
     if(erroneousResponse == null) { 
      this.erroneousResponse = "NULL Response" 
     } 
     else { 
      this.erroneousResponse = erroneousResponse 
     } 
    } 

    public Object getErroneousResponse() { 
     return this.erroneousResponse 
    } 
} 

感謝您的幫助提前!

編輯:

我也打過電話someMethod直接(http://localhost:8080/simpleTestProject/ExceptionTest/someMethod)和我看到下面的輸出:

foo... someMethod 
EXCEPTION CAUGHT - some Response with an errocode - You have seen an exception from the CONTROLLER 

這會將與服務的行爲,我期待什麼查看。

回答

2

您不應該在Grails控制器中調用另一個操作。 (的someMethod()是一個動作,因爲它是在一個控制器中的非私有方法。)

,如果你願意,你可以重定向到它,或包含它。或者你可以把它變成一個私有方法(然後它不再是一個動作,你可以繼續按照你現在的方式來調用它),或者把它移動到一個服務上。但總的來說,不會像你想要的那樣從另一個明確地調用一個動作。

基本的解釋是,操作通過默認的grails控制器添加了特殊處理。取決於你使用的是哪個版本的grails,它確實有所不同,但基本上它們將以一些常見的方式處理它們的參數和任何異常。

+0

也許這個特殊處理的鏈接會很好嗎?這不是我在文檔中看到的東西。接受作爲一種私人方法爲我工作。謝謝 –