private void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
     throws IOException, ServletException {
   ErrorWrapperResponse wrapped = new ErrorWrapperResponse(response);
   try {
     chain.doFilter(request, wrapped);
     int status = wrapped.getStatus();
     if (status >= 400) {
       handleErrorStatus(request, response, status, wrapped.getMessage());
     }
   } catch (Throwable ex) {
     handleException(request, response, wrapped, ex);
   }
 }
 private void handleException(
     HttpServletRequest request,
     HttpServletResponse response,
     ErrorWrapperResponse wrapped,
     Throwable ex)
     throws IOException, ServletException {
   Class<?> type = ex.getClass();
   String errorPath = getErrorPath(type);
   if (errorPath == null) {
     rethrow(ex);
     return;
   }
   setErrorAttributes(request, 500, ex.getMessage());
   request.setAttribute(ERROR_EXCEPTION, ex);
   request.setAttribute(ERROR_EXCEPTION_TYPE, type.getName());
   wrapped.sendError(500, ex.getMessage());
   request.getRequestDispatcher(errorPath).forward(request, response);
 }