Improved logging for DefaultDispatcherErrorHandler and
DefaultStaticContentLoader.
Provide informational logging for the processing of the two dispatcher
classes when an http sendError fails for the two known failure types
(IOException, IllegalStateException).
In both circumstances it is beneficial to have the developer aware of the
failures via the logs. These are not events that should be happening on a
regular basis, so there is little risk of a log flood.
DefaultStaticContentLoader didn't catch either exception type previously,
so changing it to make handling the same as DefaultDispatcherErrorHandler.
For DefaultDispatcherErrorHandler the IOException was caught with no
notice of failure previously. Now there will be an info level log output.
Previously the IllegalStateException was not caught, which resulted in the
unrecoverable exception being thrown up to the calling thread, usually
resulting in an ugly stacktrace to stdout/stderr. Now there will be an
info level log output instead.
If devMode is true, the info log outputs will include the exception
parameter to the log, so a stacktrace can be viewed for more details. If
devMode is false (production mode), then only the exception's tostring()
output will be produced.
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java b/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java
index 0769771..4e2af85 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java
@@ -97,6 +97,10 @@
response.sendError(code, e.getMessage());
} catch (IOException e1) {
// we're already sending an error, not much else we can do if more stuff breaks
+ LOG.info("Unable to send error response, code: " + code + "! (IOException): " + e1);
+ } catch (IllegalStateException ise) {
+ // Log illegalstate instead of passing unrecoverable exception to calling thread
+ LOG.info("Unable to send error response, code: " + code + "! isCommited: " + response.isCommitted() + " (IllegalStateException): " + ise);
}
}
@@ -122,6 +126,10 @@
response.sendError(code, "Unable to show problem report:\n" + exp + "\n\n" + LocationUtils.getLocation(exp));
} catch (IOException ex) {
// we're already sending an error, not much else we can do if more stuff breaks
+ LOG.info("Unable to send error response, code: " + code + "! (IOException): ", ex); // Stacktrace with DevMode
+ } catch (IllegalStateException ise) {
+ // Log illegalstate instead of passing unrecoverable exception to calling thread
+ LOG.info("Unable to send error response, code: " + code + "! isCommited: " + response.isCommitted() + " (IllegalStateException): ", ise); // Stacktrace with DevMode
}
}
}
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java b/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java
index eeedbf5..05a5f1b 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/DefaultStaticContentLoader.java
@@ -218,7 +218,15 @@
}
}
- response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ try {
+ response.sendError(HttpServletResponse.SC_NOT_FOUND);
+ } catch (IOException e1) {
+ // we're already sending an error, not much else we can do if more stuff breaks
+ LOG.info("Unable to send error response, code: " + HttpServletResponse.SC_NOT_FOUND + "! (IOException): " + e1);
+ } catch (IllegalStateException ise) {
+ // Log illegalstate instead of passing unrecoverable exception to calling thread
+ LOG.info("Unable to send error response, code: " + HttpServletResponse.SC_NOT_FOUND + "! isCommited: " + response.isCommitted() + " (IllegalStateException): " + ise);
+ }
}
protected void process(InputStream is, String path, HttpServletRequest request, HttpServletResponse response) throws IOException {