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

[Test] Fix parsing of exception objects #88198

Merged
merged 2 commits into from
Jul 7, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 (var 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