Fail fast when BytesRestResponse ctor throws exception (#923)

Signed-off-by: Vlad Rozov <vrozov@users.noreply.github.com>
This commit is contained in:
Vlad Rozov 2021-07-15 12:52:28 -07:00 committed by GitHub
parent 13a02becba
commit 854967ff92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 1 deletions

View File

@ -38,6 +38,8 @@ import org.opensearch.action.ActionListener;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestChannel;
import java.io.IOException;
/**
* An action listener that requires {@link #processResponse(Object)} to be implemented
* and will automatically handle failures.
@ -65,10 +67,23 @@ public abstract class RestActionListener<Response> implements ActionListener<Res
protected abstract void processResponse(Response response) throws Exception;
private BytesRestResponse from(Exception e) throws IOException {
try {
return new BytesRestResponse(channel, e);
} catch (Exception inner) {
try {
return new BytesRestResponse(channel, inner);
} finally {
inner.addSuppressed(e);
logger.error("failed to construct failure response", inner);
}
}
}
@Override
public final void onFailure(Exception e) {
try {
channel.sendResponse(new BytesRestResponse(channel, e));
channel.sendResponse(from(e));
} catch (Exception inner) {
inner.addSuppressed(e);
logger.error("failed to send failure response", inner);