From e75aab3afc55fba229b15c113f54899bb3fade46 Mon Sep 17 00:00:00 2001 From: Dimitris Athanasiou Date: Wed, 1 Aug 2018 16:40:09 +0100 Subject: [PATCH] [ML] Improve error when no available field exists for rule scope Closes #32542 --- .../xpack/core/ml/job/config/RuleScope.java | 4 ++++ .../xpack/core/ml/job/messages/Messages.java | 2 ++ .../xpack/core/ml/job/config/RuleScopeTests.java | 13 +++++++++++++ 3 files changed, 19 insertions(+) diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java index 0c633c6ead22d..3663560b978f3 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/config/RuleScope.java @@ -84,6 +84,10 @@ public boolean isEmpty() { public void validate(Set validKeys) { Optional invalidKey = scope.keySet().stream().filter(k -> !validKeys.contains(k)).findFirst(); if (invalidKey.isPresent()) { + if (validKeys.isEmpty()) { + throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS, + invalidKey.get())); + } throw ExceptionsHelper.badRequestException(Messages.getMessage(Messages.JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD, invalidKey.get(), validKeys)); } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java index 259d2d06a9c6e..3c3b1b6e56c4a 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/core/ml/job/messages/Messages.java @@ -95,6 +95,8 @@ public final class Messages { "Invalid detector rule: function {0} does not support rules with conditions"; public static final String JOB_CONFIG_DETECTION_RULE_REQUIRES_SCOPE_OR_CONDITION = "Invalid detector rule: at least scope or a condition is required"; + public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_NO_AVAILABLE_FIELDS = + "Invalid detector rule: scope field ''{0}'' is invalid; detector has no available fields for scoping"; public static final String JOB_CONFIG_DETECTION_RULE_SCOPE_HAS_INVALID_FIELD = "Invalid detector rule: scope field ''{0}'' is invalid; select from {1}"; public static final String JOB_CONFIG_FIELDNAME_INCOMPATIBLE_FUNCTION = "field_name cannot be used with function ''{0}''"; diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java index 10b9c29aba7ef..126e668dba765 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/ml/job/config/RuleScopeTests.java @@ -10,6 +10,8 @@ import org.elasticsearch.common.util.set.Sets; import org.elasticsearch.test.AbstractWireSerializingTestCase; +import java.util.Collections; + import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.is; @@ -53,6 +55,17 @@ public void testValidate_GivenMultipleValidFields() { scope.validate(Sets.newHashSet("foo", "bar", "foobar")); } + public void testValidate_GivenNoAvailableFieldsForScope() { + RuleScope scope = RuleScope.builder() + .include("foo", "filter1") + .build(); + assertThat(scope.isEmpty(), is(false)); + + ElasticsearchStatusException e = expectThrows(ElasticsearchStatusException.class, () -> scope.validate(Collections.emptySet())); + assertThat(e.getMessage(), equalTo("Invalid detector rule: scope field 'foo' is invalid; " + + "detector has no available fields for scoping")); + } + public void testValidate_GivenMultipleFieldsIncludingInvalid() { RuleScope scope = RuleScope.builder() .include("foo", "filter1")