Skip to content

Commit

Permalink
[Test] Fix parsing of exception objects
Browse files Browse the repository at this point in the history
The exception parsing logic expects the starting curly bracket already
be consumed. This PR ensures the expectation is met.

Resolves: elastic#88166
  • Loading branch information
ywangd committed Jun 30, 2022
1 parent aa6f31f commit c491df8
Showing 1 changed file with 6 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.test.rest.ESRestTestCase.entityAsMap;

public class TestSecurityClient {
Expand Down Expand Up @@ -389,7 +390,7 @@ public TokenInvalidation invalidateTokens(String requestBody) throws IOException
return new TokenInvalidation(
((Number) responseBody.get("invalidated_tokens")).intValue(),
((Number) responseBody.get("previously_invalidated_tokens")).intValue(),
errors == null ? List.of() : errors.stream().map(this::toException).toList()
errors == null ? List.of() : errors.stream().map(TestSecurityClient::toException).toList()
);
}

Expand Down Expand Up @@ -463,11 +464,10 @@ private XContentParser getParser(Response response) throws IOException {
return XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, responseBody);
}

private ElasticsearchException toException(Map<String, ?> map) {
try {
return ElasticsearchException.fromXContent(
XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map))
);
private static ElasticsearchException toException(Map<String, ?> map) {
try (final XContentParser parser = XContentType.JSON.xContent().createParser(XContentParserConfiguration.EMPTY, toJson(map))) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
return ElasticsearchException.fromXContent(parser);
} catch (IOException e) {
throw new RuntimeIoException(e);
}
Expand Down

0 comments on commit c491df8

Please sign in to comment.