Throw a more meaningful message when no document is specified for indexing

This commit is contained in:
Lee Hinman 2013-02-05 19:38:00 -07:00 committed by Simon Willnauer
parent a52e01f3e5
commit ed43ad07d7
2 changed files with 23 additions and 0 deletions

View File

@ -537,6 +537,11 @@ public class DocumentMapper implements ToXContent {
throw (MapperParsingException) e;
}
// Throw a more meaningful message if the document is empty.
if (source.source() != null && source.source().length() == 0) {
throw new MapperParsingException("failed to parse, document is empty");
}
throw new MapperParsingException("failed to parse", e);
} finally {
// only close the parser when its not provided externally

View File

@ -24,6 +24,7 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.DocumentMapperParser;
import org.elasticsearch.index.mapper.MapperParsingException;
import org.elasticsearch.index.mapper.Uid;
import org.elasticsearch.test.unit.index.mapper.MapperTests;
import org.testng.annotations.Test;
@ -113,4 +114,21 @@ public class SimpleMapperTests {
DocumentMapper builtDocMapper = MapperTests.newParser().parse(builtMapping);
assertThat((String) builtDocMapper.meta().get("param1"), equalTo("value1"));
}
@Test
public void testNoDocumentSent() throws Exception {
DocumentMapperParser mapperParser = MapperTests.newParser();
DocumentMapper docMapper = doc("test",
rootObject("person")
.add(object("name").add(stringField("first").store(true).index(false)))
).build(mapperParser);
BytesReference json = new BytesArray("".getBytes());
try {
docMapper.parse("person", "1", json).rootDoc();
assertThat("this point is never reached", false);
} catch (MapperParsingException e) {
assertThat(e.getMessage(), equalTo("failed to parse, document is empty"));
}
}
}