Skip to content

DG-12631 improvements - #2

Closed
patrick-webs wants to merge 2 commits into
HackAttack:masterfrom
patrick-webs:DG-12631-improvements
Closed

patrick-webs wants to merge 2 commits into
HackAttack:masterfrom
patrick-webs:DG-12631-improvements

Conversation

@patrick-webs

Copy link
Copy Markdown
Contributor

I found that this code in in http-builder-0.7.2-sources.jar!/groovyx/net/http/HTTPBuilder.java:

finally {
	HttpEntity entity = resp.getEntity();
	if ( entity != null ) entity.consumeContent();
}

prevents accessing the content of the response using code such as

// HttpResponseException ex;
String body = EntityUtils.toString(ex.getResponse().getEntity(), Charsets.UTF_8);

It fails with a java.io.IOException: Attempted read from closed stream error.

It seems like the solution is to use a different response handler that doesn't throw an groovyx.net.http.HttpResponseException, but rather throws an exception that includes the body content as a string. After making these changes I was successfully able to get the full HTTP response body from an exception thrown by the async client in my own app.

The Async client was suppressing the response body before calling
codecould look at it; this was complicating the debugging of a HTTP 404
Not Found that the client was receiving from CloudFlare.
@HackAttack

Copy link
Copy Markdown
Owner

I was able to take a different approach and get the async version to behave the same as the synchronous version upon an exception, namely making the parsed response available via a call to getData(). The only difference is it’s wrapped in an ExecutionException as per the contract of Future.

Future future = // some async call
try {
    future.get();
} catch (ExecutionException ex) {
    HttpResponseException unwrapped = (HttpResponseException) ex.getCause();
    Map parsedErrorResponse = (Map) unwrapped.getResponse().getData();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants