Skip to content
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

ResponseStatusExceptionFilter: Fix unwrap of JSON message #1223

Merged
merged 1 commit into from Jul 16, 2019

Conversation

hugares
Copy link
Contributor

@hugares hugares commented Jul 16, 2019

Reading the entity stream a second time without resetting it was causing
the ObjectMapper to return null then a NullPointerExceptions when
calling get('message') method on it.

Fix the issue by creating the ObjectMapper from the String
representation of the entity stream instead of resetting the stream to
read it a second time. Also check to make sure the returned JsonNode is
not null and that the content is text.

Closes #1222


This change is Reviewable

@@ -80,9 +80,12 @@ private String getBodyAsMessage(ClientResponseContext responseContext) {

if (MediaType.APPLICATION_JSON_TYPE.equals(mediaType)) {
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(entityStream).get("message");
JsonNode node = mapper.readTree(message);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe surround with try? Parsing here is optional and shouldn't fail response exceptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@codecov-io
Copy link

codecov-io commented Jul 16, 2019

Codecov Report

Merging #1223 into master will decrease coverage by 0.01%.
The diff coverage is 50%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1223      +/-   ##
==========================================
- Coverage   57.97%   57.96%   -0.02%     
==========================================
  Files         455      455              
  Lines        8833     8838       +5     
  Branches      531      532       +1     
==========================================
+ Hits         5121     5123       +2     
- Misses       3435     3437       +2     
- Partials      277      278       +1
Impacted Files Coverage Δ
...va/jaxrs/filter/ResponseStatusExceptionFilter.java 54.05% <50%> (+4.05%) ⬆️
...va/org/apache/http/impl/io/ChunkedInputStream.java 57.14% <0%> (-1.91%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 5c3ba9a...2010582. Read the comment docs.

}
}
} catch (IOException ignored) {
//ignore parsing errors and return the message as is
Copy link
Member

@KostyaSha KostyaSha Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please log to debug/trace logger, that may help to identify issue and fix in future

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I also logged the stack trace. let me know if you prefer to log only the error message

@@ -31,6 +33,8 @@
*/
public class ResponseStatusExceptionFilter implements ClientResponseFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(ResponseStatusExceptionFilter.class.getName());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

without .getName() just class is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -31,6 +33,8 @@
*/
public class ResponseStatusExceptionFilter implements ClientResponseFilter {

private static final Logger LOGGER = LoggerFactory.getLogger(ResponseStatusExceptionFilter.class.getName());
Copy link
Member

@KostyaSha KostyaSha Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOG (less symbols on line)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, there is 59 places using LOG vs 147 for LOGGER, should we consider changing the others for consistency (In a separate PR of course)?

Copy link
Member

@KostyaSha KostyaSha Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to minimise new lines i'm using shorter variant. No need to change old code, not critical.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Reading the entity stream a second time without resetting it was causing
the ObjectMapper to return null then a NullPointerExceptions when
calling get('message') method on it.

Fix the issue by creating the ObjectMapper from the String
representation of the entity stream instead of resetting the stream to
read it a second time. Also check to make sure the returned JsonNode is
not null and that the content is text.

Closes docker-java#1222
@KostyaSha KostyaSha merged commit 1c5a9e1 into docker-java:master Jul 16, 2019
@hugares hugares deleted the fix-issue-1222 branch July 19, 2019 11:53
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.

NotFoundException message is empty since 3.1.3
3 participants