Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved UI error messages when the parameter must be a constant #74893

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure we can just lift code from https://stackoverflow.com/questions/6810336/is-there-a-way-in-java-to-convert-an-integer-to-its-ordinal-name/6810409#6810409 - it's CC-BY-SA and we're dual SSPL/Elastic v2. I don't know.


private void replaceCallWithConstant(
InvokeCallMemberNode irInvokeCallMemberNode,
Consumer<ExpressionNode> scope,
Expand All @@ -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"
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down