Skip to content

Commit

Permalink
Fix #5939 unwrap ServletException
Browse files Browse the repository at this point in the history
Fix #5939 unwrap ServletException

Signed-off-by: Greg Wilkins <gregw@webtide.com>
  • Loading branch information
gregw committed Feb 8, 2021
1 parent 4c67b88 commit 5f784b6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,20 @@ private enum PageLookupTechnique
THROWABLE, STATUS_CODE, GLOBAL
}

protected ServletContext _servletContext;
private final Map<String, String> _errorPages = new HashMap<>(); // code or exception to URL
private final List<ErrorCodeRange> _errorPageList = new ArrayList<>(); // list of ErrorCode by range
protected ServletContext _servletContext;
private boolean _unwrapServletException = true;

public boolean isUnwrapServletException()
{
return _unwrapServletException;
}

public void setUnwrapServletException(boolean unwrapServletException)
{
_unwrapServletException = unwrapServletException;
}

@Override
public String getErrorPage(HttpServletRequest request)
Expand All @@ -53,14 +64,15 @@ public String getErrorPage(HttpServletRequest request)
PageLookupTechnique pageSource = null;

Class<?> matchedThrowable = null;
Throwable th = (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
Throwable error = (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
Throwable cause = error;

// Walk the cause hierarchy
while (errorPage == null && th != null)
while (errorPage == null && cause != null)
{
pageSource = PageLookupTechnique.THROWABLE;

Class<?> exClass = th.getClass();
Class<?> exClass = cause.getClass();
errorPage = _errorPages.get(exClass.getName());

// walk the inheritance hierarchy
Expand All @@ -75,7 +87,17 @@ public String getErrorPage(HttpServletRequest request)
if (errorPage != null)
matchedThrowable = exClass;

th = (th instanceof ServletException) ? ((ServletException)th).getRootCause() : null;
cause = (cause instanceof ServletException) ? ((ServletException)cause).getRootCause() : null;
}

if (error instanceof ServletException && _unwrapServletException)
{
Throwable unwrapped = ((ServletException)error).getRootCause();
if (unwrapped != null)
{
request.setAttribute(Dispatcher.ERROR_EXCEPTION, unwrapped);
request.setAttribute(Dispatcher.ERROR_EXCEPTION_TYPE, unwrapped.getClass());
}
}

Integer errorStatusCode = null;
Expand Down Expand Up @@ -129,7 +151,7 @@ public String getErrorPage(HttpServletRequest request)
Throwable originalThrowable = (Throwable)request.getAttribute(Dispatcher.ERROR_EXCEPTION);
dbg.append(originalThrowable.getClass().getName());
dbg.append(')');
LOG.debug(dbg.toString(), th);
LOG.debug(dbg.toString(), cause);
break;
case STATUS_CODE:
dbg.append(" (from status code ");
Expand Down Expand Up @@ -225,9 +247,9 @@ protected void doStart() throws Exception

private static class ErrorCodeRange
{
private int _from;
private int _to;
private String _uri;
private final int _from;
private final int _to;
private final String _uri;

ErrorCodeRange(int from, int to, String uri)
throws IllegalArgumentException
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ public void testErrorException() throws Exception
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: javax.servlet.ServletException: java.lang.IllegalStateException"));
assertThat(response, Matchers.containsString("ERROR_EXCEPTION_TYPE: class javax.servlet.ServletException"));
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$FailServlet-"));
assertThat(response, Matchers.containsString("ERROR_REQUEST_URI: /fail/exception"));
}
Expand Down Expand Up @@ -619,7 +619,7 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response) t
if (code != null)
response.sendError(Integer.parseInt(code));
else
throw new ServletException(new IllegalStateException());
throw new ServletException(new IllegalStateException("Test Exception"));
}
}

Expand Down

0 comments on commit 5f784b6

Please sign in to comment.