Skip to content

Commit

Permalink
Fail fast when BytesRestResponse ctor throws exception (#923)
Browse files Browse the repository at this point in the history
Signed-off-by: Vlad Rozov <vrozov@users.noreply.github.com>
  • Loading branch information
vrozov authored Jul 15, 2021
1 parent 13a02be commit 854967f
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
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.
Expand Down Expand Up @@ -65,10 +67,23 @@ public final void onResponse(Response response) {

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);
Expand Down

0 comments on commit 854967f

Please sign in to comment.