Simplify exception handling in PubSubMessageProcessor Remove the catch-log-rethrow pattern in getCommand(). Catch JsonParseException narrowly around getCommand() in handle() and return a failed future, so that MessageReceiverProvider's exceptionally() handler takes care of logging and nacking. Change-Id: I7986b5cd6f670b76dee4753f261d10926a6a6964
diff --git a/src/main/java/com/googlesource/gerrit/plugins/highavailability/gcp/PubSubMessageProcessor.java b/src/main/java/com/googlesource/gerrit/plugins/highavailability/gcp/PubSubMessageProcessor.java index 7eb1749..4e3fafe 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/highavailability/gcp/PubSubMessageProcessor.java +++ b/src/main/java/com/googlesource/gerrit/plugins/highavailability/gcp/PubSubMessageProcessor.java
@@ -20,6 +20,7 @@ import com.google.common.flogger.FluentLogger; import com.google.gerrit.extensions.registration.DynamicItem; import com.google.gson.Gson; +import com.google.gson.JsonParseException; import com.google.inject.Inject; import com.google.inject.Singleton; import com.google.pubsub.v1.PubsubMessage; @@ -40,21 +41,17 @@ public CompletableFuture<Boolean> handle(PubsubMessage msg) { log.atFine().log("Processing message: %s", msg); + Command command; try { - return processor.get().handle(getCommand(msg)); - } catch (RuntimeException e) { - // getCommand() throws on deserialization failure; already logged there - return CompletableFuture.completedFuture(false); + command = getCommand(msg); + } catch (JsonParseException e) { + return CompletableFuture.failedFuture(e); } + return processor.get().handle(command); } - private Command getCommand(PubsubMessage msg) { - try { - String s = msg.getData().toStringUtf8(); - return gson.fromJson(s, Command.class); - } catch (RuntimeException e) { - log.atSevere().withCause(e).log("Error parsing message %s", msg); - throw e; - } + private Command getCommand(PubsubMessage msg) throws JsonParseException { + String s = msg.getData().toStringUtf8(); + return gson.fromJson(s, Command.class); } }