-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature: option to retrieve original json body if parse exception occurred #886
Changes from 3 commits
51976d1
35c07e9
9835bc4
0e93afd
d003cc6
5bcf666
9832695
890a76b
cdeb192
e7eee8a
30ee626
435d2ef
0beeebb
d93cd29
d416490
a7b422e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -45,6 +45,8 @@ | |||||||||
import jakarta.json.stream.JsonParser; | ||||||||||
|
||||||||||
import javax.annotation.Nullable; | ||||||||||
import java.io.ByteArrayInputStream; | ||||||||||
import java.io.ByteArrayOutputStream; | ||||||||||
import java.io.IOException; | ||||||||||
import java.io.InputStream; | ||||||||||
import java.nio.ByteBuffer; | ||||||||||
|
@@ -377,28 +379,42 @@ private <ResponseT> ResponseT decodeTransportResponse( | |||||||||
) throws IOException { | ||||||||||
|
||||||||||
if (endpoint instanceof JsonEndpoint) { | ||||||||||
|
||||||||||
// Expecting a body | ||||||||||
if (entity == null) { | ||||||||||
throw new TransportException( | ||||||||||
clientResp, | ||||||||||
"Expecting a response body, but none was sent", | ||||||||||
endpoint.id() | ||||||||||
); | ||||||||||
} | ||||||||||
InputStream content = entity.asInputStream(); | ||||||||||
InputStream contentForException = null; | ||||||||||
|
||||||||||
// if the option to print the original body has been set, the body has to be | ||||||||||
// copied first to another stream to be read again by the exception class | ||||||||||
if(options().retrieveOriginalJsonResponseOnException()) { | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We already have something similar for error responses: Lines 336 to 339 in 5d466ad
We could do the same here. Or even factorize it in a new method |
||||||||||
try(ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | ||||||||||
entity.writeTo(baos); | ||||||||||
content = new ByteArrayInputStream(baos.toByteArray()); | ||||||||||
contentForException = new ByteArrayInputStream(baos.toByteArray()); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
@SuppressWarnings("unchecked") | ||||||||||
JsonEndpoint<?, ResponseT, ?> jsonEndpoint = (JsonEndpoint<?, ResponseT, ?>) endpoint; | ||||||||||
// Successful response | ||||||||||
ResponseT response = null; | ||||||||||
JsonpDeserializer<ResponseT> responseParser = jsonEndpoint.responseDeserializer(); | ||||||||||
if (responseParser != null) { | ||||||||||
// Expecting a body | ||||||||||
if (entity == null) { | ||||||||||
throw new TransportException( | ||||||||||
clientResp, | ||||||||||
"Expecting a response body, but none was sent", | ||||||||||
endpoint.id() | ||||||||||
); | ||||||||||
} | ||||||||||
checkJsonContentType(entity.contentType(), clientResp, endpoint); | ||||||||||
try ( | ||||||||||
InputStream content = entity.asInputStream(); | ||||||||||
JsonParser parser = mapper.jsonProvider().createParser(content) | ||||||||||
) { | ||||||||||
response = responseParser.deserialize(parser, mapper); | ||||||||||
} catch (Exception e) { | ||||||||||
throw new TransportException( | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. custom exception to avoid changing too many lines around |
||||||||||
throw new TransportBodyResponseException( | ||||||||||
contentForException, | ||||||||||
clientResp, | ||||||||||
"Failed to decode response", | ||||||||||
endpoint.id(), | ||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package co.elastic.clients.transport; | ||
|
||
import co.elastic.clients.transport.http.TransportHttpClient; | ||
|
||
import javax.annotation.Nullable; | ||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
|
||
public class TransportBodyResponseException extends TransportException { | ||
|
||
private String originalBody; | ||
|
||
public TransportBodyResponseException(InputStream originalBody,TransportHttpClient.Response response, String message, String endpointId, | ||
Throwable cause) { | ||
super(response, message, endpointId, cause); | ||
try { | ||
if (originalBody != null) { | ||
StringBuilder sb = new StringBuilder(); | ||
BufferedReader br = new BufferedReader(new InputStreamReader(originalBody)); | ||
String read; | ||
|
||
while ((read=br.readLine()) != null) { | ||
sb.append(read); | ||
} | ||
|
||
br.close(); | ||
this.originalBody = sb.toString(); | ||
// Closing original body input stream | ||
originalBody.close(); | ||
} | ||
|
||
// Make sure the response is closed to free up resources. | ||
response.close(); | ||
} catch (Exception e) { | ||
this.addSuppressed(e); | ||
} | ||
} | ||
|
||
/** | ||
* The original response body, before json deserialization. | ||
*/ | ||
@Nullable | ||
public String originalBody() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Having a different kind of exception depending on the "capture body" flag is a bit confusing. Another approach would be to add a new implementation of |
||
return originalBody; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ public class RestClientOptions implements TransportOptions { | |
|
||
private final RequestOptions options; | ||
|
||
boolean retrieveOriginalJsonResponseOnException; | ||
|
||
@VisibleForTesting | ||
static final String CLIENT_META_VALUE = getClientMeta(); | ||
@VisibleForTesting | ||
|
@@ -99,6 +101,15 @@ public Function<List<String>, Boolean> onWarnings() { | |
return warnings -> options.getWarningsHandler().warningsShouldFailRequest(warnings); | ||
} | ||
|
||
@Override | ||
public boolean retrieveOriginalJsonResponseOnException() { | ||
return this.retrieveOriginalJsonResponseOnException; | ||
} | ||
|
||
public void setRetrieveOriginalJsonResponseOnException(boolean retrieveOriginalJsonResponseOnException) { | ||
this.retrieveOriginalJsonResponseOnException = retrieveOriginalJsonResponseOnException; | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return new Builder(options.toBuilder()); | ||
|
@@ -181,6 +192,11 @@ public TransportOptions.Builder onWarnings(Function<List<String>, Boolean> liste | |
return this; | ||
} | ||
|
||
@Override | ||
public TransportOptions.Builder retrieveOriginalJsonResponseOnException(boolean value) { | ||
return this; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it empty? It should set a boolean flag in the builder, and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it cannot set a boolean flag in the builder, because the builder is in the RestClient code |
||
} | ||
|
||
@Override | ||
public RestClientOptions build() { | ||
return new RestClientOptions(addBuiltinHeaders(builder).build()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,6 +64,11 @@ public Function<List<String>, Boolean> onWarnings() { | |
return null; | ||
} | ||
|
||
@Override | ||
public boolean retrieveOriginalJsonResponseOnException() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Rename it to something shorter like |
||
return false; | ||
} | ||
|
||
@Override | ||
public Builder toBuilder() { | ||
return null; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a new field here, that is initialized from the builder's
keepResponseBodyOnException
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is the default one, shouldn't it be false by default?