From 3a0bc504a9f31bc4c23738887267ba457db950a4 Mon Sep 17 00:00:00 2001 From: Alexander Reelsen Date: Tue, 2 May 2017 16:01:45 +0200 Subject: [PATCH] Watcher: Exeuting a watch returns a proper 404 (elastic/x-pack-elasticsearch#1273) A test failure uncovered that handling non existing watches in the execute watch API could lead to 500 errors instead of 404 because they were not handled correctly. relates elastic/x-pack-elasticsearch#1120 Original commit: elastic/x-pack-elasticsearch@c17a07ae3e15c5b07c3ef80a35f3858cd81ebfd8 --- .../actions/ack/TransportAckWatchAction.java | 2 +- .../execute/TransportExecuteWatchAction.java | 13 +++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/ack/TransportAckWatchAction.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/ack/TransportAckWatchAction.java index 43c61f54f40..7e4b2a62ecc 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/ack/TransportAckWatchAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/ack/TransportAckWatchAction.java @@ -53,7 +53,7 @@ public class TransportAckWatchAction extends WatcherTransportAction listener) { client.getWatch(request.getWatchId(), ActionListener.wrap((response) -> { if (response.isExists() == false) { - listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exit", request.getWatchId())); + listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exist", request.getWatchId())); } else { DateTime now = new DateTime(clock.millis(), UTC); Watch watch = parser.parseWithSecrets(request.getWatchId(), true, response.getSourceAsBytesRef(), now, XContentType.JSON); diff --git a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java index f75f9ae3ca7..ded2cc30940 100644 --- a/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java +++ b/plugin/src/main/java/org/elasticsearch/xpack/watcher/transport/actions/execute/TransportExecuteWatchAction.java @@ -7,6 +7,7 @@ package org.elasticsearch.xpack.watcher.transport.actions.execute; import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.logging.log4j.util.Supplier; +import org.elasticsearch.ResourceNotFoundException; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; @@ -70,10 +71,14 @@ public class TransportExecuteWatchAction extends WatcherTransportAction listener) { if (request.getId() != null) { client.getWatch(request.getId(), ActionListener.wrap(response -> { - Watch watch = watchParser.parse(request.getId(), true, response.getSourceAsBytesRef(), request.getXContentType()); - watch.version(response.getVersion()); - watch.status().version(response.getVersion()); - executeWatch(request, listener, watch, true); + if (response.isExists()) { + Watch watch = watchParser.parse(request.getId(), true, response.getSourceAsBytesRef(), request.getXContentType()); + watch.version(response.getVersion()); + watch.status().version(response.getVersion()); + executeWatch(request, listener, watch, true); + } else { + listener.onFailure(new ResourceNotFoundException("Watch with id [{}] does not exist", request.getId())); + } }, listener::onFailure)); } else if (request.getWatchSource() != null) { try {