SOLR-2402 -- more verbose errors from DocumentBuilder

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1078928 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2011-03-07 20:34:03 +00:00
parent b5dbbf5e29
commit 16b1b1d6c9
2 changed files with 115 additions and 71 deletions

View File

@ -206,6 +206,15 @@ public class DocumentBuilder {
} }
} }
private static String getID( SolrInputDocument doc, IndexSchema schema )
{
String id = "";
SchemaField sf = schema.getUniqueKeyField();
if( sf != null ) {
id = "[doc="+doc.getFieldValue( sf.getName() )+"] ";
}
return id;
}
/** /**
* Convert a SolrInputDocument to a lucene Document. * Convert a SolrInputDocument to a lucene Document.
@ -235,19 +244,15 @@ public class DocumentBuilder {
// Make sure it has the correct number // Make sure it has the correct number
if( sfield!=null && !sfield.multiValued() && field.getValueCount() > 1 ) { if( sfield!=null && !sfield.multiValued() && field.getValueCount() > 1 ) {
String id = "";
SchemaField sf = schema.getUniqueKeyField();
if( sf != null ) {
id = "["+doc.getFieldValue( sf.getName() )+"] ";
}
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
"ERROR: "+id+"multiple values encountered for non multiValued field " + "ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued field " +
sfield.getName() + ": " +field.getValue() ); sfield.getName() + ": " +field.getValue() );
} }
// load each field value // load each field value
boolean hasField = false; boolean hasField = false;
try {
for( Object v : field ) { for( Object v : field ) {
if( v == null ) { if( v == null ) {
continue; continue;
@ -286,7 +291,7 @@ public class DocumentBuilder {
// check if the copy field is a multivalued or not // check if the copy field is a multivalued or not
if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) { if (!destinationField.multiValued() && out.get(destinationField.getName()) != null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"ERROR: multiple values encountered for non multiValued copy field " + "ERROR: "+getID(doc, schema)+"multiple values encountered for non multiValued copy field " +
destinationField.getName() + ": " + val); destinationField.getName() + ": " + val);
} }
@ -315,11 +320,17 @@ public class DocumentBuilder {
// first field. // first field.
boost = 1.0f; boost = 1.0f;
} }
}
catch( Exception ex ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
"ERROR: "+getID(doc, schema)+"Error adding field '" +
field.getName() + "'='" +field.getValue()+"'", ex );
}
// make sure the field was used somehow... // make sure the field was used somehow...
if( !used && hasField ) { if( !used && hasField ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"ERROR:unknown field '" + throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
name + "'"); "ERROR: "+getID(doc, schema)+"unknown field '" +name + "'");
} }
} }
@ -332,8 +343,7 @@ public class DocumentBuilder {
addField(out, field, field.getDefaultValue(), 1.0f); addField(out, field, field.getDefaultValue(), 1.0f);
} }
else { else {
String id = schema.printableUniqueKey( out ); String msg = getID(doc, schema) + "missing required field: " + field.getName();
String msg = "Document ["+id+"] missing required field: " + field.getName();
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, msg ); throw new SolrException( SolrException.ErrorCode.BAD_REQUEST, msg );
} }
} }

View File

@ -66,6 +66,40 @@ public class DocumentBuilderTest extends SolrTestCaseJ4 {
assertNull( out.get( "name" ) ); assertNull( out.get( "name" ) );
} }
@Test
public void testExceptions()
{
SolrCore core = h.getCore();
// make sure a null value is not indexed
SolrInputDocument doc = new SolrInputDocument();
doc.addField( "id", "123", 1.0f );
doc.addField( "unknown", "something", 1.0f );
try {
DocumentBuilder.toDocument( doc, core.getSchema() );
fail( "added an unknown field" );
}
catch( Exception ex ) {
assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
}
doc.remove( "unknown" );
doc.addField( "weight", "not a number", 1.0f );
try {
DocumentBuilder.toDocument( doc, core.getSchema() );
fail( "invalid 'float' field value" );
}
catch( Exception ex ) {
assertTrue( "should have document ID", ex.getMessage().indexOf( "doc=123" ) > 0 );
assertTrue( "cause is number format", ex.getCause() instanceof NumberFormatException );
}
// now make sure it is OK
doc.setField( "weight", "1.34", 1.0f );
DocumentBuilder.toDocument( doc, core.getSchema() );
}
@Test @Test
public void testMultiField() throws Exception { public void testMultiField() throws Exception {
SolrCore core = h.getCore(); SolrCore core = h.getCore();