Index: modules/cnr-mongo-mdstore/trunk/src/test/java/eu/dnetlib/data/mdstore/modular/mongodb/IndexFieldParserTest.java
===================================================================
--- modules/cnr-mongo-mdstore/trunk/src/test/java/eu/dnetlib/data/mdstore/modular/mongodb/IndexFieldParserTest.java	(revision 47516)
+++ modules/cnr-mongo-mdstore/trunk/src/test/java/eu/dnetlib/data/mdstore/modular/mongodb/IndexFieldParserTest.java	(revision 47517)
@@ -1,10 +1,5 @@
 package eu.dnetlib.data.mdstore.modular.mongodb;
 
-import eu.dnetlib.data.mdstore.modular.MDFormatDescription;
-import eu.dnetlib.data.mdstore.modular.mongodb.utils.IndexFieldRecordParser;
-import org.apache.commons.io.IOUtils;
-import org.junit.Test;
-
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.ArrayList;
@@ -11,6 +6,13 @@
 import java.util.List;
 import java.util.Map;
 
+import eu.dnetlib.data.mdstore.MDStoreServiceException;
+import eu.dnetlib.data.mdstore.modular.MDFormatDescription;
+import eu.dnetlib.data.mdstore.modular.mongodb.utils.IndexFieldRecordParser;
+import eu.dnetlib.data.mdstore.modular.mongodb.utils.IndexFieldRecordParserException;
+import org.apache.commons.io.IOUtils;
+import org.junit.Test;
+
 /**
  * Created by sandro on 11/29/16.
  */
@@ -18,7 +20,7 @@
 
 
     @Test
-    public void parserTest() throws IOException {
+    public void parserTest() throws IOException, MDStoreServiceException, IndexFieldRecordParserException {
         InputStream inputStream = this.getClass().getResourceAsStream("/eu/dnetlib/data/mdstore/modular/mongodb/inputRecord.xml");
 
         final String inputRecord = IOUtils.toString(inputStream);
Index: modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/IndexFieldRecordParserException.java
===================================================================
--- modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/IndexFieldRecordParserException.java	(nonexistent)
+++ modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/IndexFieldRecordParserException.java	(revision 47517)
@@ -0,0 +1,25 @@
+package eu.dnetlib.data.mdstore.modular.mongodb.utils;
+
+/**
+ * Created by Alessia Bardi on 14/06/2017.
+ *
+ * @author Alessia Bardi
+ */
+public class IndexFieldRecordParserException extends Exception {
+
+	public IndexFieldRecordParserException() {
+		super();
+	}
+
+	public IndexFieldRecordParserException(final String message) {
+		super(message);
+	}
+
+	public IndexFieldRecordParserException(final String message, final Throwable cause) {
+		super(message, cause);
+	}
+
+	public IndexFieldRecordParserException(final Throwable cause) {
+		super(cause);
+	}
+}
Index: modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/IndexFieldRecordParser.java
===================================================================
--- modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/IndexFieldRecordParser.java	(revision 47516)
+++ modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/utils/IndexFieldRecordParser.java	(revision 47517)
@@ -1,5 +1,10 @@
 package eu.dnetlib.data.mdstore.modular.mongodb.utils;
 
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 import com.ximpleware.AutoPilot;
 import com.ximpleware.VTDGen;
 import com.ximpleware.VTDNav;
@@ -7,11 +12,6 @@
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
 /**
  * Created by sandro on 11/29/16.
  */
@@ -19,7 +19,7 @@
 
     private static final Log log = LogFactory.getLog(IndexFieldRecordParser.class);
 
-    public static List<String> getTextValue(final AutoPilot ap, final VTDNav vn, final String xpath) throws Exception {
+    private static List<String> getTextValue(final AutoPilot ap, final VTDNav vn, final String xpath) throws Exception {
         List<String> results = new ArrayList<>();
         ap.selectXPath(xpath);
         while (ap.evalXPath() != -1) {
@@ -29,7 +29,7 @@
         return results;
     }
 
-    public Map<String, List<String>> parseRecord(final String record, final List<MDFormatDescription> mdformats) {
+    public Map<String, List<String>> parseRecord(final String record, final List<MDFormatDescription> mdformats) throws IndexFieldRecordParserException {
         if (mdformats == null || mdformats.size() == 0)
             return null;
         final Map<String, List<String>> result = new HashMap<>();
@@ -46,11 +46,9 @@
                 result.put(description.getName(), xpathResult);
             }
             return result;
-
         } catch (Throwable e) {
-            log.error("Error on parsing record " + record, e);
+            throw new IndexFieldRecordParserException("Cannot index record", e);
         }
-        return result;
     }
 
 
Index: modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MongoBulkWritesManager.java
===================================================================
--- modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MongoBulkWritesManager.java	(revision 47516)
+++ modules/cnr-mongo-mdstore/trunk/src/main/java/eu/dnetlib/data/mdstore/modular/mongodb/MongoBulkWritesManager.java	(revision 47517)
@@ -1,5 +1,8 @@
 package eu.dnetlib.data.mdstore.modular.mongodb;
 
+import java.util.List;
+import java.util.Map;
+
 import com.google.common.collect.Lists;
 import com.mongodb.BasicDBObject;
 import com.mongodb.DBObject;
@@ -6,23 +9,22 @@
 import com.mongodb.WriteConcern;
 import com.mongodb.client.MongoCollection;
 import com.mongodb.client.model.*;
+import eu.dnetlib.data.mdstore.MDStoreServiceException;
 import eu.dnetlib.data.mdstore.modular.MDFormatDescription;
 import eu.dnetlib.data.mdstore.modular.RecordParser;
 import eu.dnetlib.data.mdstore.modular.mongodb.utils.IndexFieldRecordParser;
+import eu.dnetlib.data.mdstore.modular.mongodb.utils.IndexFieldRecordParserException;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 
-import java.util.List;
-import java.util.Map;
-
 public class MongoBulkWritesManager {
 
 	private static final Log log = LogFactory.getLog(MongoBulkWritesManager.class);
 	private final boolean discardRecords;
-    private final boolean indexRecords;
-    private final IndexFieldRecordParser indexFieldRecordParser = new IndexFieldRecordParser();
-    private final List<MDFormatDescription> mdref;
-    private RecordParser recordParser;
+	private final boolean indexRecords;
+	private final IndexFieldRecordParser indexFieldRecordParser = new IndexFieldRecordParser();
+	private final List<MDFormatDescription> mdref;
+	private RecordParser recordParser;
 	private MongoCollection<DBObject> validCollection;
 	private List<WriteModel<DBObject>> validBulkOperationList;
 	private BulkWriteOptions writeOptions;
@@ -31,11 +33,11 @@
 	private int bulkSize;
 
 	public MongoBulkWritesManager(final MongoCollection<DBObject> collection,
-                                  final MongoCollection<DBObject> discardedCollection,
-                                  final List<MDFormatDescription> mdref,
-                                  final int bulkSize,
-                                  final RecordParser parser,
-                                  final boolean discardRecords) {
+			final MongoCollection<DBObject> discardedCollection,
+			final List<MDFormatDescription> mdref,
+			final int bulkSize,
+			final RecordParser parser,
+			final boolean discardRecords) {
 		this.validCollection = collection.withWriteConcern(WriteConcern.ACKNOWLEDGED);
 		this.validBulkOperationList = Lists.newArrayList();
 
@@ -45,37 +47,16 @@
 		this.bulkSize = bulkSize;
 		this.recordParser = parser;
 		this.discardRecords = discardRecords;
-        this.mdref = mdref;
+		this.mdref = mdref;
 
-        this.indexRecords = this.mdref != null;
-        this.writeOptions = new BulkWriteOptions().ordered(false);
+		this.indexRecords = this.mdref != null;
+		this.writeOptions = new BulkWriteOptions().ordered(false);
 	}
 
-	public void insert(final String record) {
+	public void insert(final String record) throws MDStoreServiceException {
+		Map<String, String> recordProperties = null;
 		try {
-			final Map<String, String> recordProperties = recordParser.parseRecord(record);
-            Map<String, List<String>> indexRecordField = null;
-            if (indexRecords) {
-                indexRecordField = indexFieldRecordParser.parseRecord(record, mdref);
-            }
-
-			log.debug("found props: " + recordProperties);
-			if (recordProperties.containsKey("id")) {
-                final DBObject obj = buildDBObject(record, recordProperties, indexRecordField);
-                if (log.isDebugEnabled()) {
-					log.debug("Saving object" + obj);
-				}
-				validBulkOperationList.add(new ReplaceOneModel(new BasicDBObject("id", obj.get("id")), obj, new UpdateOptions().upsert(true)));
-				if (((validBulkOperationList.size() % bulkSize) == 0) && !validBulkOperationList.isEmpty()) {
-					validCollection.bulkWrite(validBulkOperationList, writeOptions);
-					validBulkOperationList.clear();
-				}
-			} else {
-				if (discardRecords) {
-					log.debug("parsed record seems invalid");
-					discardRecord(record);
-				}
-			}
+			recordProperties = recordParser.parseRecord(record);
 		} catch (Throwable e) {
 			if (discardRecords) {
 				log.debug("unhandled exception: " + e.getMessage());
@@ -82,6 +63,33 @@
 				discardRecord(record);
 			}
 		}
+		Map<String, List<String>> indexRecordField = null;
+		try {
+			if (indexRecords) {
+				indexRecordField = indexFieldRecordParser.parseRecord(record, mdref);
+			}
+		} catch (IndexFieldRecordParserException e) {
+			// could not index record fields
+			throw new MDStoreServiceException("Are you using the correct type of store / index definition for the records in " + validCollection.getNamespace() + " ?", e);
+		}
+
+		log.debug("found props: " + recordProperties);
+		if (recordProperties.containsKey("id")) {
+			final DBObject obj = buildDBObject(record, recordProperties, indexRecordField);
+			if (log.isDebugEnabled()) {
+				log.debug("Saving object" + obj);
+			}
+			validBulkOperationList.add(new ReplaceOneModel(new BasicDBObject("id", obj.get("id")), obj, new UpdateOptions().upsert(true)));
+			if (((validBulkOperationList.size() % bulkSize) == 0) && !validBulkOperationList.isEmpty()) {
+				validCollection.bulkWrite(validBulkOperationList, writeOptions);
+				validBulkOperationList.clear();
+			}
+		} else {
+			if (discardRecords) {
+				log.debug("parsed record seems invalid");
+				discardRecord(record);
+			}
+		}
 	}
 
 	private void discardRecord(final String record) {
@@ -109,15 +117,15 @@
 		discardedCollection = getCollectionWithWriteConcern(discardedCollection, WriteConcern.ACKNOWLEDGED);
 	}
 
-    private DBObject buildDBObject(final String record, final Map<String, String> recordProperties, final Map<String, List<String>> indexFieldProperties) {
-        final DBObject obj = new BasicDBObject();
+	private DBObject buildDBObject(final String record, final Map<String, String> recordProperties, final Map<String, List<String>> indexFieldProperties) {
+		final DBObject obj = new BasicDBObject();
 		obj.put("id", recordProperties.get("id"));
 		obj.put("originalId", recordProperties.get("originalId"));
 		obj.put("body", record);
 		obj.put("timestamp", System.currentTimeMillis());
-        if (indexFieldProperties != null)
-            obj.putAll(indexFieldProperties);
-        return obj;
+		if (indexFieldProperties != null)
+			obj.putAll(indexFieldProperties);
+		return obj;
 	}
 
 	private MongoCollection<DBObject> getCollectionWithWriteConcern(MongoCollection<DBObject> collection, WriteConcern writeConcern) {
