Whenever I use the XContentBuilder like such -
try {
contentBuilder.startObject().startObject();
//some logic, adding queries, etc
contentBuilder.endObject().endObject();
} catch (Exception e) {
//do something
} finally {
if (contentBuilder != null) contentBuilder.close();
}
If an exception is thrown in the logic between the "startObject" and "endObject" call, contentBuilder.close() throws an exception, "Failed to close XContentBuilder".
So the XContentBuilder, which is a Closeable, does not get closed.. does anyone have a way around this? Doing all this logic beforehand and passing only valid data to the XContentBuilder would be much messier. Shouldn't the close() method simply release the resource without doing any validations?