You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by sb...@apache.org on 2009/03/25 23:05:51 UTC

svn commit: r758447 - in /httpcomponents/httpclient/trunk: ./ httpclient/src/main/java/org/apache/http/client/ httpclient/src/main/java/org/apache/http/impl/client/ httpclient/src/test/java/org/apache/http/impl/client/

Author: sberlin
Date: Wed Mar 25 22:05:45 2009
New Revision: 758447

URL: http://svn.apache.org/viewvc?rev=758447&view=rev
Log:
include the cause of a NonRepeatableRequestException when failing.

Modified:
    httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/NonRepeatableRequestException.java
    httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
    httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java

Modified: httpcomponents/httpclient/trunk/RELEASE_NOTES.txt
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/RELEASE_NOTES.txt?rev=758447&r1=758446&r2=758447&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/RELEASE_NOTES.txt (original)
+++ httpcomponents/httpclient/trunk/RELEASE_NOTES.txt Wed Mar 25 22:05:45 2009
@@ -1,6 +1,10 @@
 Changes since 4.0 beta 2
 -------------------
 
+* NonRepeatableRequestExceptions now include the cause that the original
+  request failed.
+  Contributed by Sam Berlin <sberlin at apache.org>
+
 * [HTTPCLIENT-837] Fixed problem with the wire log skipping zero byte values 
   if read one byte at a time.
   Contributed by Kirill Safonov <ksafonov at swiftteams.com>

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/NonRepeatableRequestException.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/NonRepeatableRequestException.java?rev=758447&r1=758446&r2=758447&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/NonRepeatableRequestException.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/client/NonRepeatableRequestException.java Wed Mar 25 22:05:45 2009
@@ -62,4 +62,16 @@
         super(message);
     }
 
+    /**
+     * Creates a new NonRepeatableEntityException with the specified detail message.
+     * 
+     * @param message The exception detail message
+     * @param cause the cause
+     */
+    public NonRepeatableRequestException(String message, Throwable cause) {
+        super(message, cause);
+    }
+    
+    
+
 }

Modified: httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java?rev=758447&r1=758446&r2=758447&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/main/java/org/apache/http/impl/client/DefaultRequestDirector.java Wed Mar 25 22:05:45 2009
@@ -392,14 +392,21 @@
                 requestExec.preProcess(wrapper, httpProcessor, context);
                 
                 boolean retrying = true;
+                Exception retryReason = null;
                 while (retrying) {
                     // Increment total exec count (with redirects)
                     execCount++;
                     // Increment exec count for this particular request
                     wrapper.incrementExecCount();
                     if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) {
-                        throw new NonRepeatableRequestException("Cannot retry request " +
-                                "with a non-repeatable request entity");
+                        if(retryReason != null) {
+                            throw new NonRepeatableRequestException("Cannot retry request " +
+                                "with a non-repeatable request entity.  The cause lists the " +
+                                "reason the original request failed.", retryReason);
+                        } else {
+                            throw new NonRepeatableRequestException("Cannot retry request " +
+                                    "with a non-repeatable request entity.");
+                        }
                     }
                     
                     try {
@@ -422,6 +429,7 @@
                                 this.log.debug(ex.getMessage(), ex);
                             }
                             this.log.info("Retrying request");
+                            retryReason = ex;
                         } else {
                             throw ex;
                         }

Modified: httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java?rev=758447&r1=758446&r2=758447&view=diff
==============================================================================
--- httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java (original)
+++ httpcomponents/httpclient/trunk/httpclient/src/test/java/org/apache/http/impl/client/TestDefaultClientRequestDirector.java Wed Mar 25 22:05:45 2009
@@ -638,9 +638,15 @@
     }
 
     private static class FaultyHttpRequestExecutor extends HttpRequestExecutor {
-
+        
         private static final String MARKER = "marker";
         
+        private final String failureMsg;
+
+        public FaultyHttpRequestExecutor(String failureMsg) {
+            this.failureMsg = failureMsg;
+        }
+        
         @Override
         public HttpResponse execute(
                 final HttpRequest request, 
@@ -650,7 +656,7 @@
             Object marker = context.getAttribute(MARKER);
             if (marker == null) {
                 context.setAttribute(MARKER, Boolean.TRUE);
-                throw new IOException("Oppsie");
+                throw new IOException(failureMsg);
             }
             return super.execute(request, conn, context);
         }
@@ -658,10 +664,20 @@
     }
     
     private static class FaultyHttpClient extends DefaultHttpClient {
+        
+        private final String failureMsg;
+        
+        public FaultyHttpClient() {
+            this("Oppsie");
+        }
+
+        public FaultyHttpClient(String failureMsg) {
+            this.failureMsg = failureMsg;
+        }
 
         @Override
         protected HttpRequestExecutor createRequestExecutor() {
-            return new FaultyHttpRequestExecutor();
+            return new FaultyHttpRequestExecutor(failureMsg);
         }
         
     }
@@ -716,11 +732,13 @@
         assertEquals(1, myheaders.length);
     }
     
-    public void testNonRepatableEntity() throws Exception {
+    public void testNonRepeatableEntity() throws Exception {
         int port = this.localServer.getServicePort();
         this.localServer.register("*", new SimpleService());
         
-        FaultyHttpClient client = new FaultyHttpClient(); 
+        String failureMsg = "a message showing that this failed";
+        
+        FaultyHttpClient client = new FaultyHttpClient(failureMsg); 
         HttpContext context = new BasicHttpContext();
 
         String s = "http://localhost:" + port;
@@ -735,7 +753,9 @@
             fail("ClientProtocolException should have been thrown");
         } catch (ClientProtocolException ex) {
             assertTrue(ex.getCause() instanceof NonRepeatableRequestException);
-           // expected
+            NonRepeatableRequestException nonRepeat = (NonRepeatableRequestException)ex.getCause();
+            assertTrue(nonRepeat.getCause() instanceof IOException);
+            assertEquals(failureMsg, nonRepeat.getCause().getMessage());
         }
     }