SOLR-7094: Better error reporting of JSON parse issues when indexing docs

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1658595 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy Potter 2015-02-09 22:41:29 +00:00
parent 12b30db6ce
commit f3279129a6
2 changed files with 30 additions and 23 deletions

View File

@ -132,6 +132,9 @@ Other Changes
* SOLR-6500: Refactor FileFetcher in SnapPuller, add debug logging. * SOLR-6500: Refactor FileFetcher in SnapPuller, add debug logging.
(Ramkumar Aiyengar via Mark Miller) (Ramkumar Aiyengar via Mark Miller)
* SOLR-7094: Better error reporting of JSON parse issues when indexing docs
(Ishan Chattopadhyaya via Timothy Potter)
================== 5.0.0 ================== ================== 5.0.0 ==================
Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release. Consult the LUCENE_CHANGES.txt file for additional, low level, changes in this release.

View File

@ -164,7 +164,7 @@ public class JsonLoader extends ContentStreamLoader {
processor.processRollback( parseRollback() ); processor.processRollback( parseRollback() );
} }
else { else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown command: "+v+" ["+parser.getPosition()+"]" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown command '"+v+"' at ["+parser.getPosition()+"]" );
} }
break; break;
} }
@ -175,8 +175,8 @@ public class JsonLoader extends ContentStreamLoader {
case JSONParser.BIGNUMBER: case JSONParser.BIGNUMBER:
case JSONParser.BOOLEAN: case JSONParser.BOOLEAN:
case JSONParser.NULL: case JSONParser.NULL:
log.info( "can't have a value here! " log.info( "Can't have a value here. Unexpected "
+JSONParser.getEventString(ev)+" "+parser.getPosition() ); +JSONParser.getEventString(ev)+" at ["+parser.getPosition()+"]" );
case JSONParser.OBJECT_START: case JSONParser.OBJECT_START:
case JSONParser.OBJECT_END: case JSONParser.OBJECT_END:
@ -232,7 +232,7 @@ public class JsonLoader extends ContentStreamLoader {
try { try {
processor.processAdd(cmd); processor.processAdd(cmd);
} catch (IOException e) { } catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "error inserting doc",e); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error inserting document: ", e);
} }
} }
} }
@ -374,7 +374,7 @@ public class JsonLoader extends ContentStreamLoader {
} else if ("_route_".equals(key)) { } else if ("_route_".equals(key)) {
cmd.setRoute(parser.getString()); cmd.setRoute(parser.getString());
} else { } else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key: "+key+" ["+parser.getPosition()+"]" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key '"+key+"' at ["+parser.getPosition()+"]" );
} }
} }
else { else {
@ -385,7 +385,7 @@ public class JsonLoader extends ContentStreamLoader {
} }
else if( ev == JSONParser.OBJECT_END ) { else if( ev == JSONParser.OBJECT_END ) {
if( cmd.getId() == null && cmd.getQuery() == null ) { if( cmd.getId() == null && cmd.getQuery() == null ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing id or query for delete ["+parser.getPosition()+"]" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing id or query for delete at ["+parser.getPosition()+"]" );
} }
processor.processDelete(cmd); processor.processDelete(cmd);
@ -452,7 +452,8 @@ public class JsonLoader extends ContentStreamLoader {
String key = parser.getString(); String key = parser.getString();
if( "doc".equals( key ) ) { if( "doc".equals( key ) ) {
if( cmd.solrDoc != null ) { if( cmd.solrDoc != null ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "multiple docs in same add command" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Multiple documents in same"
+ " add command at ["+parser.getPosition()+"]" );
} }
ev = assertNextEvent( JSONParser.OBJECT_START ); ev = assertNextEvent( JSONParser.OBJECT_START );
cmd.solrDoc = parseDoc( ev ); cmd.solrDoc = parseDoc( ev );
@ -467,7 +468,7 @@ public class JsonLoader extends ContentStreamLoader {
boost = Float.parseFloat( parser.getNumberChars().toString() ); boost = Float.parseFloat( parser.getNumberChars().toString() );
} }
else { else {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key: "+key+" ["+parser.getPosition()+"]" ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown key '"+key+"' at ["+parser.getPosition()+"]" );
} }
} }
else { else {
@ -478,7 +479,7 @@ public class JsonLoader extends ContentStreamLoader {
} }
else if( ev == JSONParser.OBJECT_END ) { else if( ev == JSONParser.OBJECT_END ) {
if( cmd.solrDoc == null ) { if( cmd.solrDoc == null ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"missing solr document. "+parser.getPosition() ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Missing solr document at ["+parser.getPosition()+"]" );
} }
cmd.solrDoc.setDocumentBoost( boost ); cmd.solrDoc.setDocumentBoost( boost );
return cmd; return cmd;
@ -563,7 +564,7 @@ public class JsonLoader extends ContentStreamLoader {
if (ev == JSONParser.OBJECT_START) { if (ev == JSONParser.OBJECT_START) {
parseExtendedFieldValue(sif, ev); parseExtendedFieldValue(sif, ev);
} else { } else {
Object val = parseNormalFieldValue(ev); Object val = parseNormalFieldValue(ev, sif.getName());
sif.setValue(val, 1.0f); sif.setValue(val, 1.0f);
} }
} }
@ -585,12 +586,13 @@ public class JsonLoader extends ContentStreamLoader {
if( ev != JSONParser.NUMBER && if( ev != JSONParser.NUMBER &&
ev != JSONParser.LONG && ev != JSONParser.LONG &&
ev != JSONParser.BIGNUMBER ) { ev != JSONParser.BIGNUMBER ) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "boost should have number! "+JSONParser.getEventString(ev) ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Boost should have number. "
+ "Unexpected "+JSONParser.getEventString(ev)+" at ["+parser.getPosition()+"], field="+sif.getName() );
} }
boost = (float)parser.getDouble(); boost = (float)parser.getDouble();
} else if ("value".equals(label)) { } else if ("value".equals(label)) {
normalFieldValue = parseNormalFieldValue(parser.nextEvent()); normalFieldValue = parseNormalFieldValue(parser.nextEvent(), sif.getName());
} else { } else {
// If we encounter other unknown map keys, then use a map // If we encounter other unknown map keys, then use a map
if (extendedInfo == null) { if (extendedInfo == null) {
@ -598,7 +600,7 @@ public class JsonLoader extends ContentStreamLoader {
} }
// for now, the only extended info will be field values // for now, the only extended info will be field values
// we could either store this as an Object or a SolrInputField // we could either store this as an Object or a SolrInputField
Object val = parseNormalFieldValue(parser.nextEvent()); Object val = parseNormalFieldValue(parser.nextEvent(), sif.getName());
extendedInfo.put(label, val); extendedInfo.put(label, val);
} }
break; break;
@ -615,24 +617,25 @@ public class JsonLoader extends ContentStreamLoader {
return; return;
default: default:
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing JSON extended field value. Unexpected "+JSONParser.getEventString(ev) ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing JSON extended field value. "
+ "Unexpected "+JSONParser.getEventString(ev)+" at ["+parser.getPosition()+"], field="+sif.getName() );
} }
} }
} }
private Object parseNormalFieldValue(int ev) throws IOException { private Object parseNormalFieldValue(int ev, String fieldName) throws IOException {
if (ev == JSONParser.ARRAY_START) { if (ev == JSONParser.ARRAY_START) {
List<Object> val = parseArrayFieldValue(ev); List<Object> val = parseArrayFieldValue(ev, fieldName);
return val; return val;
} else { } else {
Object val = parseSingleFieldValue(ev); Object val = parseSingleFieldValue(ev, fieldName);
return val; return val;
} }
} }
private Object parseSingleFieldValue(int ev) throws IOException { private Object parseSingleFieldValue(int ev, String fieldName) throws IOException {
switch (ev) { switch (ev) {
case JSONParser.STRING: case JSONParser.STRING:
return parser.getString(); return parser.getString();
@ -648,14 +651,15 @@ public class JsonLoader extends ContentStreamLoader {
parser.getNull(); parser.getNull();
return null; return null;
case JSONParser.ARRAY_START: case JSONParser.ARRAY_START:
return parseArrayFieldValue(ev); return parseArrayFieldValue(ev, fieldName);
default: default:
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing JSON field value. Unexpected "+JSONParser.getEventString(ev) ); throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Error parsing JSON field value. "
+ "Unexpected "+JSONParser.getEventString(ev)+" at ["+parser.getPosition()+"], field="+fieldName );
} }
} }
private List<Object> parseArrayFieldValue(int ev) throws IOException { private List<Object> parseArrayFieldValue(int ev, String fieldName) throws IOException {
assert ev == JSONParser.ARRAY_START; assert ev == JSONParser.ARRAY_START;
ArrayList lst = new ArrayList(2); ArrayList lst = new ArrayList(2);
@ -664,7 +668,7 @@ public class JsonLoader extends ContentStreamLoader {
if (ev == JSONParser.ARRAY_END) { if (ev == JSONParser.ARRAY_END) {
return lst; return lst;
} }
Object val = parseSingleFieldValue(ev); Object val = parseSingleFieldValue(ev, fieldName);
lst.add(val); lst.add(val);
} }
} }