Grails - 为什么在控制器中抛出的异常暂停执行,但在服务中抛出异常呢

Grails - 为什么在控制器中抛出的异常暂停执行,但在服务中抛出异常呢

问题描述:

  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 

这会将与服务的行为,我期待什么查看。

您不应该在Grails控制器中调用另一个操作。 (的someMethod()是一个动作,因为它是在一个控制器中的非私有方法。)

,如果你愿意,你可以重定向到它,或包含它。或者你可以把它变成一个私有方法(然后它不再是一个动作,你可以继续按照你现在的方式来调用它),或者把它移动到一个服务上。但总的来说,不会像你想要的那样从另一个明确地调用一个动作。

基本的解释是,操作通过默认的grails控制器添加了特殊处理。取决于你使用的是哪个版本的grails,它确实有所不同,但基本上它们将以一些常见的方式处理它们的参数和任何异常。

+0

也许这个特殊处理的链接会很好吗?这不是我在文档中看到的东西。接受作为一种私人方法为我工作。谢谢 –