清除自定义异常的消息

时间:2013-04-28 14:06:45

标签: exception grails

在我的grails应用程序中,我有一个自定义InvalidTokenException,如下所示:

class InvalidTokenException extends Exception{  

  public InvalidTokenException() {}

  public InvalidTokenException(String message)
  {
    super(message);
  }  
}

我将按照以下方式投入我的服务:

throw new InvalidTokenException("Invalid token : '${word}'")

我在我的控制器中捕获并呈现给客户端,如下所示:

catch(e)
    {
        //send the exception to the client for rendering an error message.
        render(status: 400, text: e)
        return false //stops further execution
    }

虽然我想删除它只包含文本“Invalid token:word”而不是“Exception uk.co.litecollab.exceptions.InvalidTokenException Invalid token:word”的消息

任何想法如何做到这一点?

1 个答案:

答案 0 :(得分:1)

将其更改为render(status: 400, text: e.message),因为您目前正在利用e自动转换为调用toString()方法的字符串。直接调用getMessage()会更有意义,并且在这里做你需要的。

相关问题