include the cause of a NonRepeatableRequestException when failing.

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@758447 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Sam Berlin 2009-03-25 22:05:45 +00:00
parent bc063a1ad0
commit 165507b8d7
4 changed files with 52 additions and 8 deletions

View File

@ -1,6 +1,10 @@
Changes since 4.0 beta 2 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 * [HTTPCLIENT-837] Fixed problem with the wire log skipping zero byte values
if read one byte at a time. if read one byte at a time.
Contributed by Kirill Safonov <ksafonov at swiftteams.com> Contributed by Kirill Safonov <ksafonov at swiftteams.com>

View File

@ -62,4 +62,16 @@ public class NonRepeatableRequestException extends ProtocolException {
super(message); 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);
}
} }

View File

@ -392,14 +392,21 @@ public class DefaultRequestDirector implements RequestDirector {
requestExec.preProcess(wrapper, httpProcessor, context); requestExec.preProcess(wrapper, httpProcessor, context);
boolean retrying = true; boolean retrying = true;
Exception retryReason = null;
while (retrying) { while (retrying) {
// Increment total exec count (with redirects) // Increment total exec count (with redirects)
execCount++; execCount++;
// Increment exec count for this particular request // Increment exec count for this particular request
wrapper.incrementExecCount(); wrapper.incrementExecCount();
if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) { if (wrapper.getExecCount() > 1 && !wrapper.isRepeatable()) {
if(retryReason != null) {
throw new NonRepeatableRequestException("Cannot retry request " + throw new NonRepeatableRequestException("Cannot retry request " +
"with a non-repeatable request entity"); "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 { try {
@ -422,6 +429,7 @@ public class DefaultRequestDirector implements RequestDirector {
this.log.debug(ex.getMessage(), ex); this.log.debug(ex.getMessage(), ex);
} }
this.log.info("Retrying request"); this.log.info("Retrying request");
retryReason = ex;
} else { } else {
throw ex; throw ex;
} }

View File

@ -641,6 +641,12 @@ public class TestDefaultClientRequestDirector extends ServerTestBase {
private static final String MARKER = "marker"; private static final String MARKER = "marker";
private final String failureMsg;
public FaultyHttpRequestExecutor(String failureMsg) {
this.failureMsg = failureMsg;
}
@Override @Override
public HttpResponse execute( public HttpResponse execute(
final HttpRequest request, final HttpRequest request,
@ -650,7 +656,7 @@ public class TestDefaultClientRequestDirector extends ServerTestBase {
Object marker = context.getAttribute(MARKER); Object marker = context.getAttribute(MARKER);
if (marker == null) { if (marker == null) {
context.setAttribute(MARKER, Boolean.TRUE); context.setAttribute(MARKER, Boolean.TRUE);
throw new IOException("Oppsie"); throw new IOException(failureMsg);
} }
return super.execute(request, conn, context); return super.execute(request, conn, context);
} }
@ -659,9 +665,19 @@ public class TestDefaultClientRequestDirector extends ServerTestBase {
private static class FaultyHttpClient extends DefaultHttpClient { private static class FaultyHttpClient extends DefaultHttpClient {
private final String failureMsg;
public FaultyHttpClient() {
this("Oppsie");
}
public FaultyHttpClient(String failureMsg) {
this.failureMsg = failureMsg;
}
@Override @Override
protected HttpRequestExecutor createRequestExecutor() { protected HttpRequestExecutor createRequestExecutor() {
return new FaultyHttpRequestExecutor(); return new FaultyHttpRequestExecutor(failureMsg);
} }
} }
@ -716,11 +732,13 @@ public class TestDefaultClientRequestDirector extends ServerTestBase {
assertEquals(1, myheaders.length); assertEquals(1, myheaders.length);
} }
public void testNonRepatableEntity() throws Exception { public void testNonRepeatableEntity() throws Exception {
int port = this.localServer.getServicePort(); int port = this.localServer.getServicePort();
this.localServer.register("*", new SimpleService()); 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(); HttpContext context = new BasicHttpContext();
String s = "http://localhost:" + port; String s = "http://localhost:" + port;
@ -735,7 +753,9 @@ public class TestDefaultClientRequestDirector extends ServerTestBase {
fail("ClientProtocolException should have been thrown"); fail("ClientProtocolException should have been thrown");
} catch (ClientProtocolException ex) { } catch (ClientProtocolException ex) {
assertTrue(ex.getCause() instanceof NonRepeatableRequestException); assertTrue(ex.getCause() instanceof NonRepeatableRequestException);
// expected NonRepeatableRequestException nonRepeat = (NonRepeatableRequestException)ex.getCause();
assertTrue(nonRepeat.getCause() instanceof IOException);
assertEquals(failureMsg, nonRepeat.getCause().getMessage());
} }
} }