diff options
5 files changed, 62 insertions, 24 deletions
diff --git a/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java b/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java index be4aa6cfb41..5da6b58ac98 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java +++ b/document/src/main/java/com/yahoo/document/json/JsonFeedReader.java @@ -52,7 +52,7 @@ public class JsonFeedReader implements FeedReader { } else if (documentOperation instanceof DocumentPut) { return new DocumentFeedOperation(((DocumentPut) documentOperation).getDocument(), documentOperation.getCondition()); } else { - throw new IllegalStateException("Got unknown class from JSON reader: " + documentOperation.getClass().getName()); + throw new IllegalArgumentException("Got unknown class from JSON reader: " + documentOperation.getClass().getName()); } } diff --git a/document/src/main/java/com/yahoo/document/json/JsonReader.java b/document/src/main/java/com/yahoo/document/json/JsonReader.java index d512fd3a6d1..79e435b6d9d 100644 --- a/document/src/main/java/com/yahoo/document/json/JsonReader.java +++ b/document/src/main/java/com/yahoo/document/json/JsonReader.java @@ -49,7 +49,7 @@ public class JsonReader { parser = parserFactory.createParser(input); } catch (IOException e) { state = END_OF_FEED; - throw new RuntimeException(e); + throw new IllegalArgumentException(e); } } @@ -61,13 +61,13 @@ public class JsonReader { */ public DocumentOperation readSingleDocument(DocumentParser.SupportedOperation operationType, String docIdString) { DocumentId docId = new DocumentId(docIdString); - final DocumentParseInfo documentParseInfo; + DocumentParseInfo documentParseInfo; try { DocumentParser documentParser = new DocumentParser(parser); documentParseInfo = documentParser.parse(Optional.of(docId)).get(); } catch (IOException e) { state = END_OF_FEED; - throw new RuntimeException(e); + throw new IllegalArgumentException(e); } documentParseInfo.operationType = operationType; VespaJsonDocumentReader vespaJsonDocumentReader = new VespaJsonDocumentReader(); @@ -96,9 +96,9 @@ public class JsonReader { } catch (IOException r) { // Jackson is not able to recover from structural parse errors state = END_OF_FEED; - throw new RuntimeException(r); + throw new IllegalArgumentException(r); } - if (! documentParseInfo.isPresent()) { + if ( ! documentParseInfo.isPresent()) { state = END_OF_FEED; return null; } @@ -117,9 +117,8 @@ public class JsonReader { private static DocumentType getDocumentTypeFromString(String docTypeString, DocumentTypeManager typeManager) { final DocumentType docType = typeManager.getDocumentType(docTypeString); - if (docType == null) { + if (docType == null) throw new IllegalArgumentException(String.format("Document type %s does not exist", docTypeString)); - } return docType; } @@ -129,7 +128,7 @@ public class JsonReader { } catch (IOException e) { // Jackson is not able to recover from structural parse errors state = END_OF_FEED; - throw new RuntimeException(e); + throw new IllegalArgumentException(e); } } } diff --git a/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java b/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java index 28aa9ed1d8d..b57b55c9d73 100644 --- a/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java +++ b/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java @@ -20,6 +20,7 @@ import java.io.InputStream; * @author dybis */ public class SingleDocumentParser { + private static final JsonFactory jsonFactory = new JsonFactory().disable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES); private DocumentTypeManager docMan; @@ -36,12 +37,12 @@ public class SingleDocumentParser { } private FeedOperation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation) { - final JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory); - final DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId); + JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory); + DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId); try { inputStream.close(); } catch (IOException e) { - throw new RuntimeException(e); + throw new IllegalStateException(e); } if (supportedOperation == DocumentParser.SupportedOperation.PUT) { return new DocumentFeedOperation(((DocumentPut) documentOperation).getDocument(), documentOperation.getCondition()); @@ -49,4 +50,5 @@ public class SingleDocumentParser { return new DocumentUpdateFeedOperation((DocumentUpdate) documentOperation, documentOperation.getCondition()); } } + } diff --git a/document/src/main/java/com/yahoo/document/json/readers/JsonParserHelpers.java b/document/src/main/java/com/yahoo/document/json/readers/JsonParserHelpers.java index e3bfdb7bb2c..6339add222e 100644 --- a/document/src/main/java/com/yahoo/document/json/readers/JsonParserHelpers.java +++ b/document/src/main/java/com/yahoo/document/json/readers/JsonParserHelpers.java @@ -6,27 +6,59 @@ import com.fasterxml.jackson.core.JsonToken; import com.google.common.base.Preconditions; public class JsonParserHelpers { + public static void expectArrayStart(JsonToken token) { - Preconditions.checkState(token == JsonToken.START_ARRAY, "Expected start of array, got %s", token); + try { + Preconditions.checkState(token == JsonToken.START_ARRAY, "Expected start of array, got %s", token); + } + catch (IllegalStateException e) { + throw new IllegalArgumentException(e); + } } public static void expectArrayEnd(JsonToken token) { - Preconditions.checkState(token == JsonToken.END_ARRAY, "Expected start of array, got %s", token); + try { + Preconditions.checkState(token == JsonToken.END_ARRAY, "Expected start of array, got %s", token); + } + catch (IllegalStateException e) { + throw new IllegalArgumentException(e); + } } public static void expectObjectStart(JsonToken token) { - Preconditions.checkState(token == JsonToken.START_OBJECT, "Expected start of JSON object, got %s", token); + try { + Preconditions.checkState(token == JsonToken.START_OBJECT, "Expected start of JSON object, got %s", token); + } + catch (IllegalStateException e) { + throw new IllegalArgumentException(e); + } } public static void expectObjectEnd(JsonToken token) { - Preconditions.checkState(token == JsonToken.END_OBJECT, "Expected end of JSON object, got %s", token); + try { + Preconditions.checkState(token == JsonToken.END_OBJECT, "Expected end of JSON object, got %s", token); + } + catch (IllegalStateException e) { + throw new IllegalArgumentException(e); + } } public static void expectCompositeEnd(JsonToken token) { - Preconditions.checkState(token.isStructEnd(), "Expected end of composite, got %s", token); + try { + Preconditions.checkState(token.isStructEnd(), "Expected end of composite, got %s", token); + } + catch (IllegalStateException e) { + throw new IllegalArgumentException(e); + } } public static void expectScalarValue(JsonToken token) { - Preconditions.checkState(token.isScalarValue(), "Expected to be scalar value, got %s", token); + try { + Preconditions.checkState(token.isScalarValue(), "Expected to be scalar value, got %s", token); + } + catch (IllegalStateException e) { + throw new IllegalArgumentException(e); + } } + } diff --git a/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/RestApi.java b/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/RestApi.java index 873b0569553..6fed0dff36d 100644 --- a/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/RestApi.java +++ b/vespaclient-container-plugin/src/main/java/com/yahoo/document/restapi/resource/RestApi.java @@ -36,6 +36,7 @@ import com.yahoo.vespaclient.ClusterList; import com.yahoo.vespaxmlparser.DocumentFeedOperation; import com.yahoo.vespaxmlparser.FeedOperation; import com.yahoo.vespaxmlparser.VespaXMLFeedReader; +import com.yahoo.yolean.Exceptions; import java.io.IOException; import java.io.OutputStream; @@ -230,10 +231,14 @@ public class RestApi extends LoggingRequestHandler { } } catch (RestApiException e) { return e.getResponse(); - } catch (Exception e2) { - // We always blame the user. This might be a bit nasty, but the parser throws various kind of exception - // types, but with nice descriptions. - return Response.createErrorResponse(400, e2.getMessage(), restUri, RestUri.apiErrorCodes.PARSER_ERROR); + } catch (IllegalArgumentException userException) { + return Response.createErrorResponse(400, Exceptions.toMessageString(userException), + restUri, + RestUri.apiErrorCodes.PARSER_ERROR); + } catch (RuntimeException systemException) { + return Response.createErrorResponse(500, Exceptions.toMessageString(systemException), + restUri, + RestUri.apiErrorCodes.PARSER_ERROR); } return new Response(200, resultJson, Optional.of(restUri)); } @@ -401,8 +406,8 @@ public class RestApi extends LoggingRequestHandler { } catch (BadRequestParameterException e) { return createInvalidParameterResponse(e.getParameter(), e.getMessage()); } - final OperationHandler.VisitResult visit = operationHandler.visit(restUri, documentSelection, options); - final ObjectNode resultNode = mapper.createObjectNode(); + OperationHandler.VisitResult visit = operationHandler.visit(restUri, documentSelection, options); + ObjectNode resultNode = mapper.createObjectNode(); visit.token.ifPresent(t -> resultNode.put(CONTINUATION, t)); resultNode.putArray(DOCUMENTS).addPOJO(visit.documentsAsJsonList); resultNode.put(PATH_NAME, restUri.getRawPath()); |