2016-01-22 24 views
0

我已經創建了一個servlet過濾器,現在無論拋出異常,原因始終是過濾器類。 我正在使用request.exception檢索ErrorController中的異常類。

堆棧跟蹤
了java.lang.RuntimeException:
在my.MyController $ _closure4.doCall(MyController.groovy:68
...
在my.MyFilter.doFilter(MyFilter .groovy作爲:38
...
在java.lang.Thread.run(Thread.java:745)
我不能例外的真正原因:/Grails:使用servlet過濾器時的錯誤異常className

那麼爲什麼request.exception.getClassName()返回MyFilter而不是MyController

MyFilter:

@CompileStatic     

類MyFilter實現過濾器{

UrlMappingsHolder grailsUrlMappingsHolder 
MyService myService 

@Override 
void init(FilterConfig filterConfig) throws ServletException { 

} 

@Override 
void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { 
    String url = ((HttpServletRequest)servletRequest).getServletPath() 
    def urlInfo = grailsUrlMappingsHolder.match(url).getParameters() 
    if (!myService.controllersToNotCheck.contains(urlInfo.controller) && !myService.sourcesExist()) { 
     throw new NoEnvironmentException() 
    } 
    filterChain.doFilter(servletRequest, servletResponse) 
} 

@Override 
void destroy() { 

} 

}

+0

您是否在MyController.groovy中檢查第68行? –

+0

@VinayPrajapati問題不在位置,其中引發exeption,但在ErrorController中,所有錯誤都重定向。當我處理這個異常時,代碼** request.exception.getClassName()**總是返回「MyFilter」(而不是「MyController」,在這種情況下),並且實際拋出什麼類的異常無關緊要。 –

+0

嗯,在異常上試試getCause()? – railsdog

回答

0

謝謝你們試圖幫助。 問題如下: Grails使用GrailsWrappedRuntimeException類封裝異常; 這一個解析stacktrace,並查找帶有.groovy擴展名的最後一個文件。 MyFilter始終是stacktrace中的最後一個groovy類(如果從頂部讀到底部),那就是爲什麼className總是MyFilter,所以它隱藏了有關拋出異常的控制器或服務的信息。 我的解決方案很簡單:我將過濾器重寫爲「MyFilter .java」:)