Skip to content

Commit

Permalink
unwrap exception until we get the first non ServletException, as this…
Browse files Browse the repository at this point in the history
… can be wrap of wrap of wrap when using ContextHandlerCollection

Signed-off-by: Olivier Lamy <[email protected]>

missed to commit the test

Signed-off-by: Olivier Lamy <[email protected]>
  • Loading branch information
olamy committed Mar 30, 2022
1 parent 325f2a2 commit c8102a5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ public String getErrorPage(HttpServletRequest request)

if (error instanceof ServletException && _unwrapServletException)
{
Throwable unwrapped = ((ServletException)error).getRootCause();
Throwable unwrapped = getFirstNonServletException(error);
if (unwrapped != null)
{
request.setAttribute(Dispatcher.ERROR_EXCEPTION, unwrapped);
Expand Down Expand Up @@ -177,6 +177,18 @@ public String getErrorPage(HttpServletRequest request)
return errorPage;
}

/**
*
* @param t the initial exception
* @return the first non {@link ServletException} from root cause chain
*/
private Throwable getFirstNonServletException(Throwable t) {
if(t instanceof ServletException && t.getCause()!=null) {
return getFirstNonServletException(t.getCause());
}
return t;
}

public Map<String, String> getErrorPages()
{
return _errorPages;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void init() throws Exception
_context.addFilter(SingleDispatchFilter.class, "/*", EnumSet.allOf(DispatcherType.class));
_context.addServlet(DefaultServlet.class, "/");
_context.addServlet(FailServlet.class, "/fail/*");
_context.addServlet(FailServletDoubleWrap.class, "/fail-double-wrap/*");
_context.addServlet(FailClosedServlet.class, "/fail-closed/*");
_context.addServlet(ErrorServlet.class, "/error/*");
_context.addServlet(AppServlet.class, "/app/*");
Expand Down Expand Up @@ -307,6 +308,14 @@ public void testErrorException() throws Exception
assertThat(response, Matchers.containsString("ERROR_EXCEPTION_TYPE: class jakarta.servlet.ServletException"));
assertThat(response, Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServlet-"));
assertThat(response, Matchers.containsString("ERROR_REQUEST_URI: /fail/exception"));
response = _connector.getResponse("GET /fail-double-wrap/exception HTTP/1.0\r\n\r\n");
assertThat(response, Matchers.containsString("HTTP/1.1 500 Server Error"));
assertThat(response, Matchers.containsString("ERROR_PAGE: /TestException"));
assertThat(response, Matchers.containsString("ERROR_CODE: 500"));
assertThat(response, Matchers.containsString("ERROR_EXCEPTION: jakarta.servlet.ServletException: jakarta.servlet.ServletException: java.lang.IllegalStateException: Test Exception"));
assertThat(response, Matchers.containsString("ERROR_EXCEPTION_TYPE: class jakarta.servlet.ServletException"));
assertThat(response, Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServletDoubleWrap-"));
assertThat(response, Matchers.containsString("ERROR_REQUEST_URI: /fail-double-wrap/exception"));
}

_errorPageErrorHandler.setUnwrapServletException(true);
Expand All @@ -320,6 +329,14 @@ public void testErrorException() throws Exception
assertThat(response, Matchers.containsString("ERROR_EXCEPTION_TYPE: class java.lang.IllegalStateException"));
assertThat(response, Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServlet-"));
assertThat(response, Matchers.containsString("ERROR_REQUEST_URI: /fail/exception"));
response = _connector.getResponse("GET /fail-double-wrap/exception HTTP/1.0\r\n\r\n");
assertThat(response, Matchers.containsString("HTTP/1.1 500 Server Error"));
assertThat(response, Matchers.containsString("ERROR_PAGE: /TestException"));
assertThat(response, Matchers.containsString("ERROR_CODE: 500"));
assertThat(response, Matchers.containsString("ERROR_EXCEPTION: java.lang.IllegalStateException: Test Exception"));
assertThat(response, Matchers.containsString("ERROR_EXCEPTION_TYPE: class java.lang.IllegalStateException"));
assertThat(response, Matchers.containsString("ERROR_SERVLET: org.eclipse.jetty.servlet.ErrorPageTest$FailServletDoubleWrap-"));
assertThat(response, Matchers.containsString("ERROR_REQUEST_URI: /fail-double-wrap/exception"));
}
}

Expand Down Expand Up @@ -637,6 +654,19 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
}
}

public static class FailServletDoubleWrap extends HttpServlet implements Servlet
{
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String code = request.getParameter("code");
if (code != null)
response.sendError(Integer.parseInt(code));
else
throw new ServletException(new ServletException(new IllegalStateException("Test Exception")));
}
}

public static class FailClosedServlet extends HttpServlet implements Servlet
{
@Override
Expand Down

0 comments on commit c8102a5

Please sign in to comment.