diff options
author | Jon Bratseth <bratseth@verizonmedia.com> | 2019-04-29 11:23:23 +0200 |
---|---|---|
committer | Jon Bratseth <bratseth@verizonmedia.com> | 2019-04-29 11:23:23 +0200 |
commit | 8cbc4e04c8caf10222e00e203cc8e0b1ea569087 (patch) | |
tree | 8363456a36c56d731d09d34aad730f17f6d0015c /document | |
parent | cbc24a53d46e64ed4a199c2eb299408ac54d15dd (diff) |
Classify errors better
Diffstat (limited to 'document')
6 files changed, 29 insertions, 23 deletions
diff --git a/document/src/main/java/com/yahoo/document/TestAndSetCondition.java b/document/src/main/java/com/yahoo/document/TestAndSetCondition.java index 78e75ba8e95..767fd6672d4 100644 --- a/document/src/main/java/com/yahoo/document/TestAndSetCondition.java +++ b/document/src/main/java/com/yahoo/document/TestAndSetCondition.java @@ -16,6 +16,7 @@ import java.util.Optional; */ @Beta public class TestAndSetCondition { + public static final TestAndSetCondition NOT_PRESENT_CONDITION = new TestAndSetCondition(); private final String conditionStr; @@ -33,9 +34,9 @@ public class TestAndSetCondition { public boolean isPresent() { return !conditionStr.isEmpty(); } /** - * Maps and optional test and set conditiong string to a TestAndSetCondition. + * Maps and optional test and set condition string to a TestAndSetCondition. * If the condition string is not present, a "not present" condition is returned - * @param conditionString test and set conditiong string (document selection) + * @param conditionString test and set condition string (document selection) * @return a TestAndSetCondition representing the condition string or a "not present" condition */ public static TestAndSetCondition fromConditionString(Optional<String> conditionString) { @@ -43,4 +44,5 @@ public class TestAndSetCondition { .map(TestAndSetCondition::new) .orElse(TestAndSetCondition.NOT_PRESENT_CONDITION); } + } diff --git a/document/src/main/java/com/yahoo/document/json/JsonReaderException.java b/document/src/main/java/com/yahoo/document/json/JsonReaderException.java index f919d00d20c..2363df30374 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonReaderException.java +++ b/document/src/main/java/com/yahoo/document/json/JsonReaderException.java @@ -7,7 +7,8 @@ import com.yahoo.document.Field; /** * @author bjorncs */ -public class JsonReaderException extends RuntimeException { +public class JsonReaderException extends IllegalArgumentException { + public final DocumentId docId; public final Field field; public final Throwable cause; @@ -32,7 +33,7 @@ public class JsonReaderException extends RuntimeException { private static String createErrorMessage(DocumentId docId, Field field, Throwable cause) { return String.format("Error in document '%s' - could not parse field '%s' of type '%s': %s", - docId, field.getName(), field.getDataType().getName(), cause.getMessage()); + docId, field.getName(), field.getDataType().getName(), cause.getMessage()); } public DocumentId getDocId() { @@ -42,4 +43,5 @@ public class JsonReaderException extends RuntimeException { public Field getField() { return field; } + } diff --git a/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java b/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java index a5af14a1cde..73be43ca9d9 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java +++ b/document/src/main/java/com/yahoo/document/json/readers/AddRemoveCreator.java @@ -60,7 +60,7 @@ public class AddRemoveCreator { List<FieldValue> arrayContents = new ArrayList<>(); ArrayReader.fillArrayUpdate(buffer, initNesting, valueType, arrayContents); if (buffer.currentToken() != JsonToken.END_ARRAY) { - throw new IllegalStateException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'."); + throw new IllegalArgumentException("Expected END_ARRAY. Got '" + buffer.currentToken() + "'."); } if (isRemove) { singleUpdate = FieldUpdate.createRemoveAll(field, arrayContents); diff --git a/document/src/main/java/com/yahoo/document/json/readers/StructReader.java b/document/src/main/java/com/yahoo/document/json/readers/StructReader.java index cab55903b76..dc2c001672d 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/StructReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/StructReader.java @@ -11,6 +11,7 @@ import com.yahoo.document.json.TokenBuffer; import static com.yahoo.document.json.readers.SingleValueReader.readSingleValue; public class StructReader { + public static void fillStruct(TokenBuffer buffer, StructuredFieldValue parent) { // do note the order of initializing initNesting and token is relevant for empty docs int initNesting = buffer.nesting(); @@ -32,12 +33,11 @@ public class StructReader { } public static Field getField(TokenBuffer buffer, StructuredFieldValue parent) { - Field f = parent.getField(buffer.currentName()); - if (f == null) { - throw new NullPointerException("Could not get field \"" + buffer.currentName() + - "\" in the structure of type \"" + parent.getDataType().getDataTypeName() + "\"."); - } - return f; + Field field = parent.getField(buffer.currentName()); + if (field == null) + throw new IllegalArgumentException("No field '" + buffer.currentName() + "' in the structure of type '" + + parent.getDataType().getDataTypeName() + "'"); + return field; } } diff --git a/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java b/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java index 2aeab67f290..e252e71407a 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java +++ b/document/src/main/java/com/yahoo/document/json/readers/VespaJsonDocumentReader.java @@ -40,6 +40,7 @@ import static com.yahoo.document.json.readers.TensorRemoveUpdateReader.createTen * @author freva */ public class VespaJsonDocumentReader { + private static final String UPDATE_REMOVE = "remove"; private static final String UPDATE_ADD = "add"; @@ -67,8 +68,8 @@ public class VespaJsonDocumentReader { throw JsonReaderException.addDocId(e, documentParseInfo.documentId); } if (documentParseInfo.create.isPresent()) { - if (!(documentOperation instanceof DocumentUpdate)) { - throw new RuntimeException("Could not set create flag on non update operation."); + if (! ( documentOperation instanceof DocumentUpdate)) { + throw new IllegalArgumentException("Could not set create flag on non update operation."); } DocumentUpdate update = (DocumentUpdate) documentOperation; update.setCreateIfNonExistent(documentParseInfo.create.get()); diff --git a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java index 15d1e859f73..9df7d1f91c1 100644 --- a/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java +++ b/document/src/test/java/com/yahoo/document/json/JsonReaderTestCase.java @@ -914,16 +914,17 @@ public class JsonReaderTestCase { } @Test - public final void misspelledFieldTest() throws IOException{ - JsonReader r = createReader(inputJson("{ 'put': 'id:unittest:smoke::whee',", + public void misspelledFieldTest() throws IOException{ + JsonReader r = createReader(inputJson( + "{ 'put': 'id:unittest:smoke::whee',", " 'fields': {", " 'smething': 'smoketest',", " 'nalle': 'bamse' }}")); DocumentParseInfo parseInfo = r.parseDocument().get(); DocumentType docType = r.readDocumentType(parseInfo.documentId); DocumentPut put = new DocumentPut(new Document(docType, parseInfo.documentId)); - exception.expect(NullPointerException.class); - exception.expectMessage("Could not get field \"smething\" in the structure of type \"smoke\"."); + exception.expect(IllegalArgumentException.class); + exception.expectMessage("No field 'smething' in the structure of type 'smoke'"); new VespaJsonDocumentReader().readPut(parseInfo.fieldsBuffer, put); } @@ -954,9 +955,9 @@ public class JsonReaderTestCase { } private void testFeedWithTestAndSetCondition(String jsonDoc) { - final ByteArrayInputStream parseInfoDoc = new ByteArrayInputStream(Utf8.toBytes(jsonDoc)); - final JsonReader reader = new JsonReader(types, parseInfoDoc, parserFactory); - final int NUM_OPERATIONS_IN_FEED = 3; + ByteArrayInputStream parseInfoDoc = new ByteArrayInputStream(Utf8.toBytes(jsonDoc)); + JsonReader reader = new JsonReader(types, parseInfoDoc, parserFactory); + int NUM_OPERATIONS_IN_FEED = 3; for (int i = 0; i < NUM_OPERATIONS_IN_FEED; i++) { DocumentOperation operation = reader.next(); @@ -1005,7 +1006,7 @@ public class JsonReaderTestCase { } @Test - public final void testFeedWithTestAndSetConditionOrderingTwo() { + public void testFeedWithTestAndSetConditionOrderingTwo() { testFeedWithTestAndSetCondition( inputJson("[", " {", @@ -1037,7 +1038,7 @@ public class JsonReaderTestCase { } @Test - public final void testFeedWithTestAndSetConditionOrderingThree() { + public void testFeedWithTestAndSetConditionOrderingThree() { testFeedWithTestAndSetCondition( inputJson("[", " {", @@ -1108,7 +1109,7 @@ public class JsonReaderTestCase { @Test(expected = IllegalArgumentException.class) public void testInvalidFieldWithoutFieldsFieldShouldFailParse() { - final String jsonData = inputJson( + String jsonData = inputJson( "[", " {", " 'remove': 'id:unittest:smoke::whee',", |