Skip to content

try/catch when attempting to deserialize ServerError from response #3466

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

Merged
merged 3 commits into from
Nov 7, 2018

Conversation

russcam
Copy link
Contributor

@russcam russcam commented Oct 29, 2018

This commit adds a try/catch block around attempting to deserialize a ServerError
from the body, since IElasticsearchResponse types withn Elasticsearch.Net
set/attempt to set the response body, irrespective of the response status code.

Move BytesResponseTests and StringResponseTests from Tests.Reproduce into
main Tests project.
Allow byte array to be passed to FixedResponseClient.

Fixes #3378

This commit adds a try/catch block around attempting to deserialize a ServerError
from the body, since IElasticsearchResponse types withn Elasticsearch.Net
set/attempt to set the response body, irrespective of the response status code.

Move BytesResponseTests and StringResponseTests from Tests.Reproduce into
main Tests project.
Allow byte array to be passed to FixedResponseClient.

Fixes #3378
using(var stream = new MemoryStream(this.Body))
serverError = ServerError.Create(stream);
return true;
try
Copy link
Member

Choose a reason for hiding this comment

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

Should the try catch happen inside ServerError.Create()? Can we use the response mimetype as a hint as well here? e.g in the nginx proxy case we already know what we got back from the server is not json.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

++ to using the MIME type.

Should the try catch happen inside ServerError.Create()

I think the try/catch should be in the Try* method, with the potential for ServerError.Create() to throw an exception if a ServerError cannot be created from the Stream. To my mind, this aligns with BCL Try* method semantics.

Copy link
Member

Choose a reason for hiding this comment

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

TryX pattern would be even better 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm in two minds about using MIME type, thinking about it further. On the one hand, if the response is JSON, then the |Content-Type header should be application/json, but I wonder if there are scenarios where it may not e.g. a proxy in front that doesn't change the header. I'm thinking it may be safer to just try irrespective of Content-Type header and catch the exception.

Copy link
Member

Choose a reason for hiding this comment

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

Its more about the case where the MIME type informs us the response is not JSON we can omit the TryCreate() all together.

Copy link
Contributor Author

@russcam russcam Nov 5, 2018

Choose a reason for hiding this comment

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

So I understand correctly, are you suggesting

  1. if the MIME type is "application/json", call ServerError.Create(); inside BytesResponse/StringResponse
  2. if the MIME type is anything else, use ServerError.TryCreate()

?

Copy link
Member

Choose a reason for hiding this comment

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

  1. Call trycreate
  2. Omit all together? A bad proxy could break this but a bad proxy is broken anyways.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Have now added that check. Would you mind taking another look?

@russcam russcam merged commit 2ef5f46 into master Nov 7, 2018
@russcam
Copy link
Contributor Author

russcam commented Nov 7, 2018

backported to 6.x in 42414ec

@russcam
Copy link
Contributor Author

russcam commented Nov 7, 2018

does not need to be ported to 5.x as 5.x has a different implementation

@russcam russcam deleted the fix/3378 branch November 7, 2018 05:33
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