diff --git a/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java b/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java index 16b3bcf01e6..8d9a7cac468 100644 --- a/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java +++ b/src/main/java/org/elasticsearch/index/mapper/attachment/AttachmentMapper.java @@ -295,6 +295,11 @@ public class AttachmentMapper implements Mapper { } } + // Throw clean exception when no content is provided Fix #23 + if (content == null) { + throw new MapperParsingException("No content is provided."); + } + Metadata metadata = new Metadata(); if (contentType != null) { metadata.add(Metadata.CONTENT_TYPE, contentType); diff --git a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java index 66cf76c21db..5fa39c59e49 100644 --- a/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java +++ b/src/test/java/org/elasticsearch/plugin/mapper/attachments/test/SimpleAttachmentIntegrationTests.java @@ -25,6 +25,7 @@ import org.elasticsearch.action.count.CountResponse; import org.elasticsearch.common.logging.ESLogger; import org.elasticsearch.common.logging.Loggers; import org.elasticsearch.common.network.NetworkUtils; +import org.elasticsearch.index.mapper.MapperParsingException; import org.elasticsearch.node.Node; import org.testng.annotations.*; @@ -133,4 +134,19 @@ public class SimpleAttachmentIntegrationTests { countResponse = node.client().count(countRequest("test").query(fieldQuery("file", "End"))).actionGet(); assertThat(countResponse.count(), equalTo(1l)); } -} \ No newline at end of file + + /** + * Test case for issue https://github.com/elasticsearch/elasticsearch-mapper-attachments/issues/23 + *
We throw a nicer exception when no content is provided + * @throws Exception + */ + @Test(expectedExceptions = MapperParsingException.class) + public void testNoContent() throws Exception { + String mapping = copyToStringFromClasspath("/org/elasticsearch/index/mapper/xcontent/test-mapping.json"); + + node.client().admin().indices().putMapping(putMappingRequest("test").type("person").source(mapping)).actionGet(); + + node.client().index(indexRequest("test").type("person") + .source(jsonBuilder().startObject().field("file").startObject().endObject())).actionGet(); + } +}