SOLR-2813: Fix HTTP error codes returned when requests contain strings that can not be parsed as numbers for Trie fields

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1202499 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Chris M. Hostetter 2011-11-16 01:27:26 +00:00
parent 67478264b5
commit 3508d03220
3 changed files with 64 additions and 18 deletions

View File

@ -428,6 +428,8 @@ Bug Fixes
* SOLR-2861: Fix extremely rare race condition on commit that can result
in a NPE (yonik)
* SOLR-2813: Fix HTTP error codes returned when requests contain strings that
can not be parsed as numbers for Trie fields. (Jeff Crump and hossman)
Other Changes
----------------------

View File

@ -89,24 +89,29 @@ final class TrieTokenizer extends Tokenizer {
this.startOfs = correctOffset(0);
this.endOfs = correctOffset(len);
String v = new String(buf, 0, len);
switch (type) {
case INTEGER:
ts.setIntValue(Integer.parseInt(v));
break;
case FLOAT:
ts.setFloatValue(Float.parseFloat(v));
break;
case LONG:
ts.setLongValue(Long.parseLong(v));
break;
case DOUBLE:
ts.setDoubleValue(Double.parseDouble(v));
break;
case DATE:
ts.setLongValue(dateField.parseMath(null, v).getTime());
break;
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field");
try {
switch (type) {
case INTEGER:
ts.setIntValue(Integer.parseInt(v));
break;
case FLOAT:
ts.setFloatValue(Float.parseFloat(v));
break;
case LONG:
ts.setLongValue(Long.parseLong(v));
break;
case DOUBLE:
ts.setDoubleValue(Double.parseDouble(v));
break;
case DATE:
ts.setLongValue(dateField.parseMath(null, v).getTime());
break;
default:
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unknown type for trie field");
}
} catch (NumberFormatException nfe) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"Invalid Number: " + v);
}
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Unable to create TrieIndexTokenizer", e);

View File

@ -22,6 +22,8 @@ import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.LinkedList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
@ -222,6 +224,43 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {
);
}
@Test
public void testClientErrorOnMalformedNumbers() throws Exception {
final String BAD_VALUE = "NOT_A_NUMBER";
ignoreException(BAD_VALUE);
final List<String> FIELDS = new LinkedList<String>();
for (String type : new String[] { "ti", "tf", "td", "tl" }) {
FIELDS.add("malformed_" + type);
}
// test that malformed numerics cause client error not server error
for (String field : FIELDS) {
try {
h.update(add( doc("id","100", field, BAD_VALUE)));
fail("Didn't encounter an error trying to add a non-number: " + field);
} catch (SolrException e) {
String msg = e.toString();
assertTrue("not an (update) client error on field: " + field +" : "+ msg,
400 <= e.code() && e.code() < 500);
assertTrue("(update) client error does not mention bad value: " + msg,
msg.contains(BAD_VALUE));
}
try {
h.query(req("q",field + ":" + BAD_VALUE));
fail("Didn't encounter an error trying to query a non-number: " + field);
} catch (SolrException e) {
String msg = e.toString();
assertTrue("not a (search) client error on field: " + field +" : "+ msg,
400 <= e.code() && e.code() < 500);
assertTrue("(search) client error does not mention bad value: " + msg,
msg.contains(BAD_VALUE));
}
}
}
@Test
public void testRequestHandlerBaseException() {
final String tmp = "BOO! ignore_exception";