Skip to content

Commit

Permalink
return an HTTP code representing the error when a suggest request fai…
Browse files Browse the repository at this point in the history
…led instead of 200
  • Loading branch information
obourgain authored and areek committed Mar 19, 2015
1 parent 0cb1504 commit 35fcdf5
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 59 deletions.
25 changes: 3 additions & 22 deletions src/main/java/org/elasticsearch/action/count/CountResponse.java
Expand Up @@ -19,16 +19,14 @@

package org.elasticsearch.action.count;

import org.elasticsearch.Version;
import java.io.IOException;
import java.util.List;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastOperationResponse;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import java.util.List;

/**
* The response of the count action.
*/
Expand Down Expand Up @@ -62,24 +60,7 @@ public boolean terminatedEarly() {
}

public RestStatus status() {
if (getFailedShards() == 0) {
if (getSuccessfulShards() == 0 && getTotalShards() > 0) {
return RestStatus.SERVICE_UNAVAILABLE;
}
return RestStatus.OK;
}
// if total failure, bubble up the status code to the response level
if (getSuccessfulShards() == 0 && getTotalShards() > 0) {
RestStatus status = RestStatus.OK;
for (ShardOperationFailedException shardFailure : getShardFailures()) {
RestStatus shardStatus = shardFailure.status();
if (shardStatus.getStatus() >= status.getStatus()) {
status = shardStatus;
}
}
return status;
}
return RestStatus.OK;
return RestStatus.status(getSuccessfulShards(), getTotalShards(), getShardFailures());
}

@Override
Expand Down
Expand Up @@ -66,24 +66,7 @@ public SearchResponse(InternalSearchResponse internalResponse, String scrollId,
}

public RestStatus status() {
if (shardFailures.length == 0) {
if (successfulShards == 0 && totalShards > 0) {
return RestStatus.SERVICE_UNAVAILABLE;
}
return RestStatus.OK;
}
// if total failure, bubble up the status code to the response level
if (successfulShards == 0 && totalShards > 0) {
RestStatus status = RestStatus.OK;
for (int i = 0; i < shardFailures.length; i++) {
RestStatus shardStatus = shardFailures[i].status();
if (shardStatus.getStatus() >= status.getStatus()) {
status = shardFailures[i].status();
}
}
return status;
}
return RestStatus.OK;
return RestStatus.status(successfulShards, totalShards, shardFailures);
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/org/elasticsearch/rest/RestStatus.java
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.rest;

import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;

Expand Down Expand Up @@ -494,4 +495,24 @@ public static RestStatus readFrom(StreamInput in) throws IOException {
public static void writeTo(StreamOutput out, RestStatus status) throws IOException {
out.writeString(status.name());
}

public static RestStatus status(int successfulShards, int totalShards, ShardOperationFailedException... failures) {
if (failures.length == 0) {
if (successfulShards == 0 && totalShards > 0) {
return RestStatus.SERVICE_UNAVAILABLE;
}
return RestStatus.OK;
}
RestStatus status = RestStatus.OK;
if (successfulShards == 0 && totalShards > 0) {
for (ShardOperationFailedException failure : failures) {
RestStatus shardStatus = failure.status();
if (shardStatus.getStatus() >= status.getStatus()) {
status = failure.status();
}
}
return status;
}
return status;
}
}
Expand Up @@ -19,6 +19,9 @@

package org.elasticsearch.rest.action.suggest;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.action.suggest.SuggestRequest;
import org.elasticsearch.action.suggest.SuggestResponse;
Expand All @@ -28,15 +31,16 @@
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.rest.*;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.BytesRestResponse;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.rest.RestResponse;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.rest.action.support.RestBuilderListener;
import org.elasticsearch.search.suggest.Suggest;

import static org.elasticsearch.rest.RestRequest.Method.GET;
import static org.elasticsearch.rest.RestRequest.Method.POST;
import static org.elasticsearch.rest.RestStatus.OK;
import static org.elasticsearch.rest.action.support.RestActions.buildBroadcastShardsHeader;

/**
*
*/
Expand Down Expand Up @@ -72,14 +76,15 @@ public void handleRequest(final RestRequest request, final RestChannel channel,
client.suggest(suggestRequest, new RestBuilderListener<SuggestResponse>(channel) {
@Override
public RestResponse buildResponse(SuggestResponse response, XContentBuilder builder) throws Exception {
RestStatus restStatus = RestStatus.status(response.getSuccessfulShards(), response.getTotalShards(), response.getShardFailures());
builder.startObject();
buildBroadcastShardsHeader(builder, response);
Suggest suggest = response.getSuggest();
if (suggest != null) {
suggest.toXContent(builder, request);
}
builder.endObject();
return new BytesRestResponse(OK, builder);
return new BytesRestResponse(restStatus, builder);
}
});
}
Expand Down
16 changes: 4 additions & 12 deletions src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java
Expand Up @@ -18,7 +18,8 @@
*/
package org.elasticsearch.snapshots;

import com.google.common.collect.ImmutableList;
import java.io.IOException;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;
Expand All @@ -28,8 +29,7 @@
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.rest.RestStatus;

import java.io.IOException;
import com.google.common.collect.ImmutableList;

/**
* Information about snapshot
Expand Down Expand Up @@ -179,15 +179,7 @@ public RestStatus status() {
if (shardFailures.size() == 0) {
return RestStatus.OK;
}
RestStatus status = RestStatus.OK;
if (successfulShards == 0 && totalShards > 0) {
for (SnapshotShardFailure shardFailure : shardFailures)
if (shardFailure.status().getStatus() > status().getStatus()) {
status = shardFailure.status();
}
return status;
}
return status;
return RestStatus.status(successfulShards, totalShards, shardFailures.toArray(new ShardOperationFailedException[shardFailures.size()]));
}

static final class Fields {
Expand Down

0 comments on commit 35fcdf5

Please sign in to comment.