Skip to content

Stop returning "es." internal exception headers as http response headers #22703

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

Conversation

javanna
Copy link
Member

@javanna javanna commented Jan 19, 2017

Move "es." internal headers to separate metadata set in ElasticsearchException and stop returning them as response headers. These headers are printed out as part of the response body already, they weren't meant to be sent back as response headers too. They were introduced to prevent having to add custom exceptions to our codebase whenever we need to throw an exception that has to hold some additional metadata that ElasticsearchException doesn't support out of the box. The header notion stays in ElasticsearchException but only for what actually needs to be returned as response header (no "es." prefix).

Also removed ESExceptionTests and moved its tests under ElasticsearchExceptionTests or ExceptionSerializationTests

Closes #17593

@javanna javanna requested a review from tlrx January 19, 2017 21:23
@javanna javanna force-pushed the enhancement/stop_returning_es_exception_headers branch from 3817f0d to 2bc7547 Compare January 19, 2017 21:26
@javanna javanna requested a review from jaymode January 19, 2017 21:26
Copy link
Member

@tlrx tlrx left a comment

Choose a reason for hiding this comment

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

Thanks @javanna, I think this is heading in the right direction. I left some minor comments but we still need to figure out what to do with the TODOs :)

I do like the move of internal headers es.* to exception metadata and I share your concerns about serializing back a parsed exception... but that shouldn't happen, neither in core nor in the high level rest client.

For a long term, I think we should also drop the metadataToXcontent() method which can lead to name conflicts in the rendered xcontent and also adds confusion if we add a metadata class attribute.

}

/**
* Adds a new header with the given key.
* This method will replace existing header if a header with the same key already exists
*/
public void addHeader(String key, List<String> value) {
//we need to enforce this otherwise bw comp doesn't work properly, as "es." was the previous criteria to split headers in two sets
if (key.startsWith("es.")) {
throw new IllegalArgumentException("exception headers must not start with [es.], found [" + key + "] instead");
Copy link
Member

Choose a reason for hiding this comment

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

Maybe remove "instead" from the exception message?

@@ -410,6 +470,8 @@ public static ElasticsearchException fromXContent(XContentParser parser) throws

String type = null, reason = null, stack = null;
ElasticsearchException cause = null;
Map<String, List<String>> metadata = new HashMap<>();
//TODO should this be a Map<String, List<String>> instead?
Map<String, Object> headers = new HashMap<>();
Copy link
Member

Choose a reason for hiding this comment

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

Yes, I changed it in #22675

Copy link
Member Author

Choose a reason for hiding this comment

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

ok I will remove the TODO then

// Everything else is considered as a header
headers.put(currentFieldName, parser.text());
//TODO here we need to parse multiple values
metadata.put(currentFieldName, Collections.singletonList(parser.text()));
Copy link
Member

Choose a reason for hiding this comment

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

I think it's OK here because the token is a value, not an array. But later in the loop we need to take care of arrays.

I think we can already do it for values and just ignore objects for now

Copy link
Member Author

Choose a reason for hiding this comment

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

Are we testing this somewhere?

}

public void testWithHeaders() throws Exception {
RestRequest request = new FakeRestRequest();
RestChannel channel = randomBoolean() ? new DetailedExceptionRestChannel(request) : new SimpleExceptionRestChannel(request);

BytesRestResponse response = new BytesRestResponse(channel, new WithHeadersException());
assertEquals(2, response.getHeaders().size());
assertThat(response.getHeaders().get("n1"), notNullValue());
Copy link
Member

Choose a reason for hiding this comment

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

this line is duplicated

Copy link
Member Author

Choose a reason for hiding this comment

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

good catch

@javanna
Copy link
Member Author

javanna commented Jan 20, 2017

For a long term, I think we should also drop the metadataToXcontent() method which can lead to name conflicts in the rendered xcontent and also adds confusion if we add a metadata class attribute.

that's correct, I think that the problem is that those additional fields are printed at the same level as the metadata (previous es. headers). They should rather be in a different object that only subclasses can write, but that is a breaking change. That said I am not sure how many clients are able to parse our exceptions entirely given how difficult we made it for them :)

@javanna
Copy link
Member Author

javanna commented Jan 23, 2017

@s1monw mind having a look too?

Copy link
Contributor

@s1monw s1monw left a comment

Choose a reason for hiding this comment

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

LGTM

@javanna javanna force-pushed the enhancement/stop_returning_es_exception_headers branch from 17ab7eb to 95b9979 Compare January 24, 2017 11:41
@javanna javanna added >bug and removed >enhancement labels Jan 24, 2017
@javanna javanna force-pushed the enhancement/stop_returning_es_exception_headers branch from 95b9979 to ea0ee3b Compare January 24, 2017 12:51
@javanna javanna merged commit 47c0e13 into elastic:master Jan 24, 2017
javanna added a commit that referenced this pull request Jan 24, 2017
…ers (#22703)

move "es." internal headers to separate metadata set in ElasticsearchException and stop returning them as response headers

Closes #17593

* [TEST] remove ESExceptionTests, move its methods to ElasticsearchExceptionTests or ExceptionSerializationTests
s1monw added a commit that referenced this pull request Jan 26, 2017
We added version dependent serialziation in #22703 which triggers a bug in
LocalTransport that misses to set the serializatoin version when an exception
is serialized. This is only relevant for tests since local transport has by-definition
the same version on both ends in production.

Closes #22784
s1monw added a commit that referenced this pull request Jan 26, 2017
We added version dependent serialziation in #22703 which triggers a bug in
LocalTransport that misses to set the serializatoin version when an exception
is serialized. This is only relevant for tests since local transport has by-definition
the same version on both ends in production.

Closes #22784
s1monw added a commit that referenced this pull request Jan 26, 2017
We added version dependent serialziation in #22703 which triggers a bug in
LocalTransport that misses to set the serializatoin version when an exception
is serialized. This is only relevant for tests since local transport has by-definition
the same version on both ends in production.

Closes #22784
@lcawl lcawl added the :Core/Infra/Core Core issues without another label label Feb 13, 2018
@lcawl lcawl removed the :Exceptions label Feb 13, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Stop returning additional HTTP headers on exceptions
5 participants