Skip to content

Commit

Permalink
Implement toXContent on ShardOpertionFailureException
Browse files Browse the repository at this point in the history
ShardOperationFailureException implementations alread provide structured
exception support but it's not yet exposed on the interface. This change
allows nice rendering of structured REST exceptions also if searches fail on
only a subset of the shards etc.

Closes elastic#11017
  • Loading branch information
s1monw committed May 13, 2015
1 parent add18a5 commit 650d616
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
Expand Up @@ -20,6 +20,7 @@
package org.elasticsearch.action;

import org.elasticsearch.common.io.stream.Streamable;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.rest.RestStatus;

import java.io.Serializable;
Expand All @@ -29,7 +30,7 @@
*
*
*/
public interface ShardOperationFailedException extends Streamable, Serializable {
public interface ShardOperationFailedException extends Streamable, Serializable, ToXContent {

/**
* The index the operation failed on. Might return <tt>null</tt> if it can't be derived.
Expand Down
Expand Up @@ -24,6 +24,8 @@
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ToXContent;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.shard.IndexShardException;
import org.elasticsearch.rest.RestStatus;

Expand Down Expand Up @@ -114,4 +116,18 @@ public void writeTo(StreamOutput out) throws IOException {
public String toString() {
return "[" + index + "][" + shardId + "] failed, reason [" + reason() + "]";
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("shard", shardId());
builder.field("index", index());
if (reason != null) {
builder.field("reason");
builder.startObject();
ElasticsearchException.toXContent(builder, params, reason);
builder.endObject();
}
return builder;

}
}
Expand Up @@ -25,10 +25,7 @@
import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.uid.Versions;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentBuilderString;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.*;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryStringQueryBuilder;
import org.elasticsearch.rest.RestRequest;
Expand Down Expand Up @@ -81,14 +78,7 @@ public static void buildBroadcastShardsHeader(XContentBuilder builder, int total
builder.startArray(Fields.FAILURES);
for (ShardOperationFailedException shardFailure : shardFailures) {
builder.startObject();
if (shardFailure.index() != null) {
builder.field(Fields.INDEX, shardFailure.index(), XContentBuilder.FieldCaseConversion.NONE);
}
if (shardFailure.shardId() != -1) {
builder.field(Fields.SHARD, shardFailure.shardId());
}
builder.field(Fields.STATUS, shardFailure.status().getStatus());
builder.field(Fields.REASON, shardFailure.reason());
shardFailure.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
}
builder.endArray();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/elasticsearch/snapshots/Snapshot.java
Expand Up @@ -255,7 +255,9 @@ public XContentBuilder toXContent(XContentBuilder builder, ToXContent.Params par
builder.field(Fields.SUCCESSFUL_SHARDS, successfulShards);
builder.startArray(Fields.FAILURES);
for (SnapshotShardFailure shardFailure : shardFailures) {
SnapshotShardFailure.toXContent(shardFailure, builder, params);
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
builder.endObject();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/org/elasticsearch/snapshots/SnapshotInfo.java
Expand Up @@ -223,7 +223,9 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
}
builder.startArray(Fields.FAILURES);
for (SnapshotShardFailure shardFailure : shardFailures) {
SnapshotShardFailure.toXContent(shardFailure, builder, params);
builder.startObject();
shardFailure.toXContent(builder, params);
builder.endObject();
}
builder.endArray();
builder.startObject(Fields.SHARDS);
Expand Down
Expand Up @@ -162,13 +162,7 @@ public String toString() {
*/
public static void toXContent(SnapshotShardFailure snapshotShardFailure, XContentBuilder builder, ToXContent.Params params) throws IOException {
builder.startObject();
if (snapshotShardFailure.nodeId != null) {
builder.field("node_id", snapshotShardFailure.nodeId);
}
builder.field("index", snapshotShardFailure.index);
builder.field("reason", snapshotShardFailure.reason);
builder.field("shard_id", snapshotShardFailure.shardId);
builder.field("status", snapshotShardFailure.status.name());
snapshotShardFailure.toXContent(builder, params);
builder.endObject();
}

Expand Down Expand Up @@ -212,4 +206,16 @@ public static SnapshotShardFailure fromXContent(XContentParser parser) throws IO
}
return snapshotShardFailure;
}

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
builder.field("index", index);
builder.field("shard_id", shardId);
builder.field("reason", reason);
if (nodeId != null) {
builder.field("node_id", nodeId);
}
builder.field("status", status.name());
return builder;
}
}

0 comments on commit 650d616

Please sign in to comment.