watcher: do not continue execution when get result doesn't exist
When attempting to a get a watch that does not exist, the listener is called once inside an if statement and the execution of the method continues as if the watch existed. This causes failures to happen including a NPE. This commit wraps the execution in a if-else to prevent this. Original commit: elastic/x-pack-elasticsearch@27f09852e7
This commit is contained in:
parent
2b92ee71bc
commit
9c09b88e9f
|
@ -70,24 +70,24 @@ public class TransportGetWatchAction extends WatcherTransportAction<GetWatchRequ
|
|||
}
|
||||
|
||||
client.getWatch(request.getId(), ActionListener.wrap(getResponse -> {
|
||||
if (getResponse.isExists() == false) {
|
||||
if (getResponse.isExists()) {
|
||||
try (XContentBuilder builder = jsonBuilder()) {
|
||||
// When we return the watch via the Get Watch REST API, we want to return the watch as was specified in the put api,
|
||||
// we don't include the status in the watch source itself, but as a separate top level field, so that
|
||||
// it indicates the the status is managed by watcher itself.
|
||||
DateTime now = new DateTime(clock.millis(), UTC);
|
||||
Watch watch = parser.parseWithSecrets(request.getId(), true, getResponse.getSourceAsBytesRef(), now);
|
||||
watch.toXContent(builder, WatcherParams.builder()
|
||||
.hideSecrets(true)
|
||||
.put(Watch.INCLUDE_STATUS_KEY, false)
|
||||
.build());
|
||||
watch.version(getResponse.getVersion());
|
||||
watch.status().version(getResponse.getVersion());
|
||||
listener.onResponse(new GetWatchResponse(watch.id(), watch.status(), builder.bytes(), XContentType.JSON));
|
||||
}
|
||||
} else {
|
||||
listener.onResponse(new GetWatchResponse(request.getId()));
|
||||
}
|
||||
|
||||
try (XContentBuilder builder = jsonBuilder()) {
|
||||
// When we return the watch via the Get Watch REST API, we want to return the watch as was specified in the put api,
|
||||
// we don't include the status in the watch source itself, but as a separate top level field, so that
|
||||
// it indicates the the status is managed by watcher itself.
|
||||
DateTime now = new DateTime(clock.millis(), UTC);
|
||||
Watch watch = parser.parseWithSecrets(request.getId(), true, getResponse.getSourceAsBytesRef(), now);
|
||||
watch.toXContent(builder, WatcherParams.builder()
|
||||
.hideSecrets(true)
|
||||
.put(Watch.INCLUDE_STATUS_KEY, false)
|
||||
.build());
|
||||
watch.version(getResponse.getVersion());
|
||||
watch.status().version(getResponse.getVersion());
|
||||
listener.onResponse(new GetWatchResponse(watch.id(), watch.status(), builder.bytes(), XContentType.JSON));
|
||||
}
|
||||
}, listener::onFailure));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue