struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [5/7] git commit: WW-3887 Extracts methods
Date Sun, 16 Feb 2014 19:29:08 GMT
WW-3887 Extracts methods


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/701f0a06
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/701f0a06
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/701f0a06

Branch: refs/heads/develop
Commit: 701f0a06d0f9ffb50ad9927e949d90e7f578a087
Parents: ef733aa
Author: Lukasz Lenart <lukaszlenart@apache.org>
Authored: Sun Feb 16 20:15:37 2014 +0100
Committer: Lukasz Lenart <lukaszlenart@apache.org>
Committed: Sun Feb 16 20:15:37 2014 +0100

----------------------------------------------------------------------
 .../DefaultDispatcherErrorHandler.java          | 108 +++++++++++--------
 1 file changed, 61 insertions(+), 47 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/701f0a06/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java
----------------------------------------------------------------------
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 b09c890..c093402 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/DefaultDispatcherErrorHandler.java
@@ -20,6 +20,10 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 
+/**
+ * Default implementation of {@link org.apache.struts2.dispatcher.DispatcherErrorHandler}
+ * which sends Error Report in devMode or {@link javax.servlet.http.HttpServletResponse#sendError}
otherwise.
+ */
 public class DefaultDispatcherErrorHandler implements DispatcherErrorHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(DefaultDispatcherErrorHandler.class);
@@ -50,61 +54,71 @@ public class DefaultDispatcherErrorHandler implements DispatcherErrorHandler
{
     public void handleError(HttpServletRequest request, HttpServletResponse response, int
code, Exception e) {
         Boolean devModeOverride = FilterDispatcher.getDevModeOverride();
         if (devModeOverride != null ? devModeOverride : devMode) {
-            if (LOG.isDebugEnabled()) {
-                LOG.debug("Exception occurred during processing request: #0", e, e.getMessage());
-            }
-            try {
+            handleErrorInDevMode(response, code, e);
+        } else {
+            sendErrorResponse(request, response, code, e);
+        }
+    }
 
-                List<Throwable> chain = new ArrayList<Throwable>();
-                Throwable cur = e;
-                chain.add(cur);
-                while ((cur = cur.getCause()) != null) {
-                    chain.add(cur);
+    protected void sendErrorResponse(HttpServletRequest request, HttpServletResponse response,
int code, Exception e) {
+        try {
+            // WW-1977: Only put errors in the request when code is a 500 error
+            if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
+                // WW-4103: Only logs error when application error occurred, not Struts error
+                if (LOG.isErrorEnabled()) {
+                    LOG.error("Exception occurred during processing request: #0", e, e.getMessage());
                 }
+                // send a http error response to use the servlet defined error handler
+                // make the exception available to the web.xml defined error page
+                request.setAttribute("javax.servlet.error.exception", e);
 
-                HashMap<String,Object> data = new HashMap<String,Object>();
-                data.put("exception", e);
-                data.put("unknown", Location.UNKNOWN);
-                data.put("chain", chain);
-                data.put("locator", new Dispatcher.Locator());
-
-                Writer writer = new StringWriter();
-                template.process(data, writer);
-
-                response.setContentType("text/html");
-                response.getWriter().write(writer.toString());
-                response.getWriter().close();
-            } catch (Exception exp) {
-                try {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Cannot show problem report!", exp);
-                    }
-                    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
-                }
+                // for compatibility
+                request.setAttribute("javax.servlet.jsp.jspException", e);
             }
-        } else {
+
+            // send the error response
+            response.sendError(code, e.getMessage());
+        } catch (IOException e1) {
+            // we're already sending an error, not much else we can do if more stuff breaks
+        }
+    }
+
+    protected void handleErrorInDevMode(HttpServletResponse response, int code, Exception
e) {
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Exception occurred during processing request: #0", e, e.getMessage());
+        }
+        try {
+            List<Throwable> chain = new ArrayList<Throwable>();
+            Throwable cur = e;
+            chain.add(cur);
+            while ((cur = cur.getCause()) != null) {
+                chain.add(cur);
+            }
+
+            Writer writer = new StringWriter();
+            template.process(createReportData(e, chain), writer);
+
+            response.setContentType("text/html");
+            response.getWriter().write(writer.toString());
+            response.getWriter().close();
+        } catch (Exception exp) {
             try {
-                // WW-1977: Only put errors in the request when code is a 500 error
-                if (code == HttpServletResponse.SC_INTERNAL_SERVER_ERROR) {
-                    // WW-4103: Only logs error when application error occurred, not Struts
error
-                    if (LOG.isErrorEnabled()) {
-                        LOG.error("Exception occurred during processing request: #0", e,
e.getMessage());
-                    }
-                    // send a http error response to use the servlet defined error handler
-                    // make the exception availible to the web.xml defined error page
-                    request.setAttribute("javax.servlet.error.exception", e);
-
-                    // for compatibility
-                    request.setAttribute("javax.servlet.jsp.jspException", e);
+                if (LOG.isDebugEnabled()) {
+                    LOG.debug("Cannot show problem report!", exp);
                 }
-
-                // send the error response
-                response.sendError(code, e.getMessage());
-            } catch (IOException e1) {
+                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
             }
         }
     }
+
+    protected HashMap<String, Object> createReportData(Exception e, List<Throwable>
chain) {
+        HashMap<String,Object> data = new HashMap<String,Object>();
+        data.put("exception", e);
+        data.put("unknown", Location.UNKNOWN);
+        data.put("chain", chain);
+        data.put("locator", new Dispatcher.Locator());
+        return data;
+    }
 }


Mime
View raw message