Skip to content

Commit

Permalink
[JENKINS-21695] - The CompressionFilter.uncaughtExceptionHandler must…
Browse files Browse the repository at this point in the history
… not attempt to write to a committed response. (#2834)

(cherry picked from commit 0c7e7bb)
  • Loading branch information
jglick authored and olivergondza committed May 2, 2017
1 parent 1c56f01 commit 161095e
Showing 1 changed file with 12 additions and 4 deletions.
Expand Up @@ -20,11 +20,18 @@
* @author Kohsuke Kawaguchi
*/
public class InstallUncaughtExceptionHandler {

private static final Logger LOGGER = Logger.getLogger(InstallUncaughtExceptionHandler.class.getName());

@Initializer
public static void init(final Jenkins j) throws IOException {
CompressionFilter.setUncaughtExceptionHandler(j.servletContext, new UncaughtExceptionHandler() {
@Override
public void reportException(Throwable e, ServletContext context, HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
if (rsp.isCommitted()) {
LOGGER.log(Level.WARNING, null, e);
return;
}
req.setAttribute("javax.servlet.error.exception",e);
try {
WebApp.get(j.servletContext).getSomeStapler()
Expand All @@ -38,10 +45,10 @@ public void reportException(Throwable e, ServletContext context, HttpServletRequ
});
try {
Thread.setDefaultUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler());
DefaultUncaughtExceptionHandler.LOGGER.log(Level.FINE, "Successfully installed a global UncaughtExceptionHandler.");
LOGGER.log(Level.FINE, "Successfully installed a global UncaughtExceptionHandler.");
}
catch (SecurityException ex) {
DefaultUncaughtExceptionHandler.LOGGER.log(Level.SEVERE,
LOGGER.log(Level.SEVERE,
"Failed to set the default UncaughtExceptionHandler. " +
"If any threads die due to unhandled coding errors then there will be no logging of this information. " +
"The lack of this diagnostic information will make it harder to track down issues which will reduce the supportability of Jenkins. " +
Expand All @@ -53,8 +60,6 @@ public void reportException(Throwable e, ServletContext context, HttpServletRequ
/** An UncaughtExceptionHandler that just logs the exception */
private static class DefaultUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {

private static final Logger LOGGER = Logger.getLogger(InstallUncaughtExceptionHandler.class.getName());

@Override
public void uncaughtException(Thread t, Throwable ex) {
// if this was an OutOfMemoryError then all bets about logging are off - but in the absence of anything else...
Expand All @@ -65,4 +70,7 @@ public void uncaughtException(Thread t, Throwable ex) {
}

}

private InstallUncaughtExceptionHandler() {}

}

0 comments on commit 161095e

Please sign in to comment.