diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java index 23213a917ccfa..a16bd4f013ee7 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/DefaultConstantFoldingOptimizationPhase.java @@ -832,6 +832,13 @@ public void visitInvokeCallMember(InvokeCallMemberNode irInvokeCallMemberNode, C } } + private String convertIntToOrdinal(int i) { + String[] suffixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" }; + int aux = i % 100; + if(aux == 11 || aux == 12 || aux == 13) return i + "th"; + return i + suffixes[i % 10]; + } + private void replaceCallWithConstant( InvokeCallMemberNode irInvokeCallMemberNode, Consumer scope, @@ -843,11 +850,11 @@ private void replaceCallWithConstant( ExpressionNode argNode = irInvokeCallMemberNode.getArgumentNodes().get(i); IRDConstant constantDecoration = argNode.getDecoration(IRDConstant.class); if (constantDecoration == null) { - // TODO find a better string to output throw irInvokeCallMemberNode.getLocation() .createError( new IllegalArgumentException( - "all arguments to [" + javaMethod.getName() + "] must be constant but the [" + (i + 1) + "] argument isn't" + "The [" + javaMethod.getName() + "] method needs constant arguments to work properly. " + + "Please provide a constant to the [" + convertIntToOrdinal(i + 1) + "] argument" ) ); } diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java index b525035886332..4240e66f90a38 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/BindingsTests.java @@ -211,7 +211,8 @@ public void testClassMethodCompileTimeOnlyVariableParams() { IllegalArgumentException.class, () -> scriptEngine.compile(null, "def a = 2; classMul(2, a)", BindingsTestScript.CONTEXT, Collections.emptyMap()) ); - assertThat(e.getMessage(), equalTo("all arguments to [classMul] must be constant but the [2] argument isn't")); + assertThat(e.getMessage(), equalTo("The [classMul] method needs constant arguments to work properly. " + + "Please provide a constant to the second argument")); } public void testClassMethodCompileTimeOnlyThrows() { @@ -240,7 +241,8 @@ public void testInstanceMethodCompileTimeOnlyVariableParams() { IllegalArgumentException.class, () -> scriptEngine.compile(null, "def a = 2; instanceMul(a, 2)", BindingsTestScript.CONTEXT, Collections.emptyMap()) ); - assertThat(e.getMessage(), equalTo("all arguments to [instanceMul] must be constant but the [1] argument isn't")); + assertThat(e.getMessage(), equalTo("The [instanceMul] method needs constant arguments to work properly. " + + "Please provide a constant to the first argument")); } public void testCompileTimeOnlyParameterFoldedToConstant() { diff --git a/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/250_grok.yml b/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/250_grok.yml index f089203374100..fbbc040a315e5 100644 --- a/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/250_grok.yml +++ b/modules/runtime-fields-common/src/yamlRestTest/resources/rest-api-spec/test/runtime_fields/250_grok.yml @@ -80,7 +80,7 @@ fetch: --- mutable pattern: - do: - catch: /all arguments to \[grok\] must be constant but the \[1\] argument isn't/ + catch: /The \[grok\] method needs constant arguments to work properly. Please provide a constant to the first argument/ search: index: http_logs body: