Skip to content

Commit

Permalink
fix NPE that can occur in genMessage of FailedShardsException
Browse files Browse the repository at this point in the history
  • Loading branch information
mfussenegger committed Feb 12, 2015
1 parent 560e09e commit e14cbeb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
14 changes: 7 additions & 7 deletions sql/src/main/java/io/crate/exceptions/FailedShardsException.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ public FailedShardsException(ShardOperationFailedException[] shardFailures) {
super(genMessage(shardFailures));
}

public FailedShardsException(ShardOperationFailedException[] shardFailures, Throwable original) {
super(genMessage(shardFailures), original);
}

private static String genMessage(ShardOperationFailedException[] shardFailures) {
StringBuilder sb;

Expand All @@ -47,18 +43,22 @@ private static String genMessage(ShardOperationFailedException[] shardFailures)
}

List<String> errors = new ArrayList<>(shardFailures.length);
String table = null;
for (ShardOperationFailedException shardFailure : shardFailures) {
if (shardFailure == null) {
continue;
}
errors.add(shardFailure.shardId()+" ( "+shardFailure.reason()+" )");
table = shardFailure.index();
}

if (errors.isEmpty() && table == null) {
return "query failed on unknown shard / table";
}
sb.append(Joiner.on(", ").join(errors));
if(shardFailures[0].index() != null){
sb.append(" of table ").append(shardFailures[0].index());
if (table != null) {
sb.append(" of table ").append(table);
}

return sb.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@

public class FailedShardsExceptionTest {

@Test
public void testThatGenMessageDoesNotRaiseNPEIfShardOperationFailedExceptionIsNull() throws Exception {
//noinspection ThrowableInstanceNeverThrown
FailedShardsException exception = new FailedShardsException(new ShardOperationFailedException[]{null});
assertThat(exception.getMessage(), is("query failed on unknown shard / table"));
}

@Test
public void testShardFailureReasonIsNull() throws Exception {
FailedShardsException exception = new FailedShardsException(new ShardOperationFailedException[]{new ShardOperationFailedException() {
Expand Down

0 comments on commit e14cbeb

Please sign in to comment.