diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2021-06-24 20:44:27 +0200 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2021-06-24 20:44:27 +0200 |
commit | 72036953cbebf1c90d881ed0f555a93d32b7189a (patch) | |
tree | 539240669aafcb3e91b4f8c6a6c3e9dba21fa0c3 /vespa-feed-client | |
parent | d39965631cae501519bbc7e4df55b1cfdcbb8018 (diff) |
All exceptional failures are now exceptions
Diffstat (limited to 'vespa-feed-client')
5 files changed, 43 insertions, 31 deletions
diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java index 2269c56cde4..07084d15059 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/HttpFeedClient.java @@ -85,15 +85,25 @@ class HttpFeedClient implements FeedClient { .thenApply(response -> toResult(request, response, documentId)); } + private enum Outcome { success, conditionNotMet, vespaFailure, transportFailure }; + + static Result.Type toResultType(Outcome outcome) { + switch (outcome) { + case success: return Result.Type.success; + case conditionNotMet: return Result.Type.conditionNotMet; + default: throw new IllegalArgumentException("No corresponding result type for '" + outcome + "'"); + } + } + static Result toResult(HttpRequest request, HttpResponse response, DocumentId documentId) { - Result.Type type; + Outcome outcome; switch (response.code()) { - case 200: type = Result.Type.success; break; - case 412: type = Result.Type.conditionNotMet; break; + case 200: outcome = Outcome.success; break; + case 412: outcome = Outcome.conditionNotMet; break; case 502: case 504: - case 507: type = Result.Type.failure; break; - default: type = null; + case 507: outcome = Outcome.vespaFailure; break; + default: outcome = Outcome.transportFailure; } String message = null; @@ -125,13 +135,16 @@ class HttpFeedClient implements FeedClient { throw new ResultParseException(documentId, e); } - if (type == null) // Not a Vespa response, but a failure in the HTTP layer. + if (outcome == Outcome.transportFailure) // Not a Vespa response, but a failure in the HTTP layer. throw new ResultParseException( documentId, "Status " + response.code() + " executing '" + request + "': " + (message == null ? new String(response.body(), UTF_8) : message)); - return new Result(type, documentId, message, trace); + if (outcome == Outcome.vespaFailure) + throw new ResultException(documentId, message, trace); + + return new Result(toResultType(outcome), documentId, message, trace); } static String getPath(DocumentId documentId) { diff --git a/vespa-feed-client/src/main/java/ai/vespa/feed/client/Result.java b/vespa-feed-client/src/main/java/ai/vespa/feed/client/Result.java index 0a036c6c1b0..7be7aadc188 100644 --- a/vespa-feed-client/src/main/java/ai/vespa/feed/client/Result.java +++ b/vespa-feed-client/src/main/java/ai/vespa/feed/client/Result.java @@ -25,8 +25,7 @@ public class Result { public enum Type { success, - conditionNotMet, - failure + conditionNotMet } public Type type() { return type; } diff --git a/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java b/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java index d8090549420..6aa0de2160c 100644 --- a/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java +++ b/vespa-feed-client/src/test/java/ai/vespa/feed/client/HttpFeedClientTest.java @@ -13,6 +13,7 @@ import java.util.function.BiFunction; import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; /** * @author jonmv @@ -55,16 +56,19 @@ class HttpFeedClientTest { return failed; } }); - Result result = client.put(id, - "json", - OperationParameters.empty() - .createIfNonExistent(true) - .testAndSetCondition("false") - .route("route") - .timeout(Duration.ofSeconds(5))) - .get(); - assertEquals("Ooops! ... I did it again.", result.resultMessage().get()); - assertEquals("I played with your heart. Got lost in the game.", result.traceMessage().get()); + ExecutionException expected = assertThrows(ExecutionException.class, + () -> client.put(id, + "json", + OperationParameters.empty() + .createIfNonExistent(true) + .testAndSetCondition("false") + .route("route") + .timeout(Duration.ofSeconds(5))) + .get()); + assertTrue(expected.getCause() instanceof ResultException); + ResultException result = (ResultException) expected.getCause(); + assertEquals("Ooops! ... I did it again.", result.getMessage()); + assertEquals("I played with your heart. Got lost in the game.", result.getTrace().get()); // Handler error is a FeedException. @@ -90,11 +94,11 @@ class HttpFeedClientTest { return failed; } }); - ExecutionException expected = assertThrows(ExecutionException.class, - () -> client.put(id, - "json", - OperationParameters.empty()) - .get()); + expected = assertThrows(ExecutionException.class, + () -> client.put(id, + "json", + OperationParameters.empty()) + .get()); assertEquals("Status 500 executing 'POST /document/v1/ns/type/docid/0': Alla ska i jorden.", expected.getCause().getMessage()); } diff --git a/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonFileFeederExample.java b/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonFileFeederExample.java index 1e616f2625a..3b633c38132 100644 --- a/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonFileFeederExample.java +++ b/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonFileFeederExample.java @@ -38,17 +38,15 @@ class JsonFileFeederExample implements Closeable { resultsReceived.incrementAndGet(); if (error != null) { log.warning("Problems with feeding document " - + error.documentId().map(DocumentId::toString).orElse("<unknown>")); - errorsReceived.incrementAndGet(); - } else if (result.type() == Result.Type.failure) { - log.warning("Problems with docID " + result.documentId() + ":" + error); + + error.documentId().map(DocumentId::toString).orElse("<unknown>") + + ": " + error); errorsReceived.incrementAndGet(); } } @Override public void onError(FeedException error) { - log.severe("Feeding failed for d: " + error.getMessage()); + log.severe("Feeding failed fatally: " + error.getMessage()); } @Override diff --git a/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonStreamFeederExample.java b/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonStreamFeederExample.java index 5cee776b244..cbe0e213907 100644 --- a/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonStreamFeederExample.java +++ b/vespa-feed-client/src/test/java/ai/vespa/feed/client/examples/JsonStreamFeederExample.java @@ -100,8 +100,6 @@ class JsonStreamFeederExample extends Thread implements AutoCloseable { if (throwable != null) { System.err.printf("Failure for '%s': %s", docId, throwable); throwable.printStackTrace(); - } else if (result.type() == Result.Type.failure) { - System.err.printf("Failure for '%s': %s", docId, result.resultMessage().orElse("<no messsage>")); } }); } catch (InterruptedException e) { |