diff options
author | Harald Musum <musum@verizonmedia.com> | 2019-04-27 11:34:58 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-27 11:34:58 +0200 |
commit | 5e655d399f9ec00d60e16dab583edc96ac1f8d16 (patch) | |
tree | fc428a7139486aa713fbb1ca5c4a9973a191c876 | |
parent | e535c8fa9c1264d7164fef7f55d10dc585e13e88 (diff) |
Revert "Separate user and system exceptions"
5 files changed, 24 insertions, 62 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 5da6b58ac98..be4aa6cfb41 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 IllegalArgumentException("Got unknown class from JSON reader: " + documentOperation.getClass().getName()); + throw new IllegalStateException("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 79e435b6d9d..d512fd3a6d1 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 IllegalArgumentException(e); + throw new RuntimeException(e); } } @@ -61,13 +61,13 @@ public class JsonReader { */ public DocumentOperation readSingleDocument(DocumentParser.SupportedOperation operationType, String docIdString) { DocumentId docId = new DocumentId(docIdString); - DocumentParseInfo documentParseInfo; + final DocumentParseInfo documentParseInfo; try { DocumentParser documentParser = new DocumentParser(parser); documentParseInfo = documentParser.parse(Optional.of(docId)).get(); } catch (IOException e) { state = END_OF_FEED; - throw new IllegalArgumentException(e); + throw new RuntimeException(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 IllegalArgumentException(r); + throw new RuntimeException(r); } - if ( ! documentParseInfo.isPresent()) { + if (! documentParseInfo.isPresent()) { state = END_OF_FEED; return null; } @@ -117,8 +117,9 @@ 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; } @@ -128,7 +129,7 @@ public class JsonReader { } catch (IOException e) { // Jackson is not able to recover from structural parse errors state = END_OF_FEED; - throw new IllegalArgumentException(e); + throw new RuntimeException(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 b57b55c9d73..28aa9ed1d8d 100644 --- a/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java +++ b/document/src/main/java/com/yahoo/document/json/SingleDocumentParser.java @@ -20,7 +20,6 @@ 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; @@ -37,12 +36,12 @@ public class SingleDocumentParser { } private FeedOperation parse(InputStream inputStream, String docId, DocumentParser.SupportedOperation supportedOperation) { - JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory); - DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId); + final JsonReader reader = new JsonReader(docMan, inputStream, jsonFactory); + final DocumentOperation documentOperation = reader.readSingleDocument(supportedOperation, docId); try { inputStream.close(); } catch (IOException e) { - throw new IllegalStateException(e); + throw new RuntimeException(e); } if (supportedOperation == DocumentParser.SupportedOperation.PUT) { return new DocumentFeedOperation(((DocumentPut) documentOperation).getDocument(), documentOperation.getCondition()); @@ -50,5 +49,4 @@ 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 6339add222e..e3bfdb7bb2c 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,59 +6,27 @@ import com.fasterxml.jackson.core.JsonToken; import com.google.common.base.Preconditions; public class JsonParserHelpers { - public static void expectArrayStart(JsonToken token) { - try { - Preconditions.checkState(token == JsonToken.START_ARRAY, "Expected start of array, got %s", token); - } - catch (IllegalStateException e) { - throw new IllegalArgumentException(e); - } + Preconditions.checkState(token == JsonToken.START_ARRAY, "Expected start of array, got %s", token); } public static void expectArrayEnd(JsonToken token) { - try { - Preconditions.checkState(token == JsonToken.END_ARRAY, "Expected start of array, got %s", token); - } - catch (IllegalStateException e) { - throw new IllegalArgumentException(e); - } + Preconditions.checkState(token == JsonToken.END_ARRAY, "Expected start of array, got %s", token); } public static void expectObjectStart(JsonToken token) { - try { - Preconditions.checkState(token == JsonToken.START_OBJECT, "Expected start of JSON object, got %s", token); - } - catch (IllegalStateException e) { - throw new IllegalArgumentException(e); - } + Preconditions.checkState(token == JsonToken.START_OBJECT, "Expected start of JSON object, got %s", token); } public static void expectObjectEnd(JsonToken token) { - try { - Preconditions.checkState(token == JsonToken.END_OBJECT, "Expected end of JSON object, got %s", token); - } - catch (IllegalStateException e) { - throw new IllegalArgumentException(e); - } + Preconditions.checkState(token == JsonToken.END_OBJECT, "Expected end of JSON object, got %s", token); } public static void expectCompositeEnd(JsonToken token) { - try { - Preconditions.checkState(token.isStructEnd(), "Expected end of composite, got %s", token); - } - catch (IllegalStateException e) { - throw new IllegalArgumentException(e); - } + Preconditions.checkState(token.isStructEnd(), "Expected end of composite, got %s", token); } public static void expectScalarValue(JsonToken token) { - try { - Preconditions.checkState(token.isScalarValue(), "Expected to be scalar value, got %s", token); - } - catch (IllegalStateException e) { - throw new IllegalArgumentException(e); - } + Preconditions.checkState(token.isScalarValue(), "Expected to be scalar value, got %s", token); } - } 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 6fed0dff36d..873b0569553 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,7 +36,6 @@ 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; @@ -231,14 +230,10 @@ public class RestApi extends LoggingRequestHandler { } } catch (RestApiException e) { return e.getResponse(); - } 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); + } 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); } return new Response(200, resultJson, Optional.of(restUri)); } @@ -406,8 +401,8 @@ public class RestApi extends LoggingRequestHandler { } catch (BadRequestParameterException e) { return createInvalidParameterResponse(e.getParameter(), e.getMessage()); } - OperationHandler.VisitResult visit = operationHandler.visit(restUri, documentSelection, options); - ObjectNode resultNode = mapper.createObjectNode(); + final OperationHandler.VisitResult visit = operationHandler.visit(restUri, documentSelection, options); + final ObjectNode resultNode = mapper.createObjectNode(); visit.token.ifPresent(t -> resultNode.put(CONTINUATION, t)); resultNode.putArray(DOCUMENTS).addPOJO(visit.documentsAsJsonList); resultNode.put(PATH_NAME, restUri.getRawPath()); |