[英]How do I handle error states in servlet filters without showing the user a stack trace?
我正在開發一個Jetty / RESTEasy應用程序。 如果我從我的一個REST端點拋出WebApplicationException(myResponse)
,它會將給定的響應發送給客戶端。
當篩選器檢測到錯誤時,我想要相同的行為:
顯然,只需寫入響應流並從doFilter
方法中return
工作。 但這對doFilter
調用的其他方法不起作用。
拋出任何異常將滿足條件#1但我還沒有想出一個理智的方式來滿足條件#2。 (你可以在底部看到我最好的嘗試。)
正如Perception在他的回答中解釋的那樣, WebApplicationException
被視為在Filter的上下文中的任何其他異常,因此給用戶一個漂亮的丑陋堆棧跟蹤。
那么,總結一下我的問題:
throw new WebApplicationException(Response)
? 我在一個過濾器中有這個代碼並且它可以工作,但我更喜歡一個更優雅的解決方案,它自動應用於所有過濾器:
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
try {
doFilterOrThrow(request, response, chain);
} catch (WebApplicationException e) {
Response res = e.getResponse();
((HttpServletResponse) response).sendError(res.getStatus(), (String) res.getEntity());
}
}
你提到的具體處理的Web應用程序異常僅一個JAX-RS的容器,其中,順便說一句,是不一樣的事,作為一個Servlet容器的范圍內定義。
Web過濾器由Servlet容器處理,該容器不知道或不關心同一應用程序服務器中是否存在JAX-RS容器。 它也不知道或不關心Web應用程序異常。 因此,當您從過濾器中拋出WAE時,它將被視為與任何其他異常相同(帶有堆棧跟蹤的服務器錯誤,或者如果您在Web應用程序中設置了一個預配置的錯誤頁面)。
在我看來,如果您通過過濾器向客戶端指示錯誤,您可以直接寫入響應流。 但是,如果您嘗試利用一些現有的JAX-RS邏輯,那么(RESTEasy特定的)解決方案是將請求標記為過濾器中的錯誤,然后使用提供程序類在JAX-RS中生成WAE。 例:
@WebFilter(urlPatterns = "*")
public class ForwardingFilter implements Filter {
@Override
public void destroy() {
return;
}
@Override
public void doFilter(final ServletRequest request,
final ServletResponse response, final FilterChain chain)
throws IOException, ServletException {
// Add an error response to be processed by the JAX-RS container.
// This would obviously be based on some condition.
request.setAttribute("errorResponse",
Response.status(500).entity("Didn't work out!").build());
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
return;
}
}
@Provider
@ServerInterceptor
@HeaderDecoratorPrecedence
@RequestScoped
public class ForwardingHandlerProvider implements PreProcessInterceptor {
@Override
public ServerResponse preProcess(final HttpRequest request,
final ResourceMethod method) throws Failure,
WebApplicationException {
final Response errorResponse = (Response) request
.getAttribute("errorResponse");
if (errorResponse != null)
throw new WebApplicationException(errorResponse);
return null;
}
}
由於提供程序存在於JAX-RS域中,因此Web應用程序異常將根據JAX-RS規范的第3.3.4節的規則進行處理,並在客戶端獲得所需的響應。
*編輯:*
最重要的是,沒有標准的Java EE規定方式(當前)以集中方式處理servlet異常,類似於JAX-RS中可用的方式。 既然你正在使用JBoss / RestEASY,你可以利用JBoss Seam Catch庫來獲得非常接近的結果。
@HandlesExceptions
public class ExceptionHandler {
public void handleServletException(
final @Handles @WebRequest CaughtException<ServletException> caught,
@Context final HttpServletResponse response) {
try {
response.sendError(500, "An error occured");
} catch (final IOException ioe) {
System.err.println("Dumb IO Exception: " + ioe);
}
}
}
上面說明了一個異常處理程序,如Seam Catch文檔中所述 。 請注意,圖書館目前處於巨大變化之中,因此您只想將其作為最后的手段使用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.