Grails定制marshaller异常处理

时间:2014-08-29 07:47:34

标签: grails

我正在为Grails 2.3.7编写的RESTful应用程序编写自定义编组程序,该程序调用服务方法调用:

class CommentMarshaller implements MarshallerRegistrar {

    @Autowired
    RatingService ratingService

    @Override
    void register() {
        JSON.registerObjectMarshaller(Comment) { Comment comment ->
            def total = ratingService.getSumRating(comment)
            if (!total) {
                throw new IllegalStateException("to be caught")
            }
            return [
                id: comment.id,
                created: comment.created,
                text: comment.text,
                authorName: comment.author.username,
                authorId: comment.author.id,
                rating: total
            ]
        }
    }
}

我试图捕获异常,这些异常是在我的ErrorController中从marshaller抛出的:

class UrlMappings {
    static mappings = {
        "500"(controller: "error", action: "handleError")
    }
}

当控制器代码中发生某些错误时,它可以正常工作,但是当从marshaller抛出异常时,Grails会尝试查找error.gsp并在那里渲染堆栈跟踪。 如何将错误从marshaller重定向到ErrorController?

0 个答案:

没有答案
相关问题