elastic / elastic/elasticsearch

Painless: Improve Error Messages for methods missing in painless but existing in underlying classes

Open
#79,062 4 comments 0 reactions 0 assignees View on GitHub
:Core/Infra/Scripting >enhancement Team:Core/Infra
Dominant language
Java
Stars
77.9k
Forks
26.1k
PR merge metrics
PR metrics pending

Description

Sometimes painless decides to implement a subset of methods to not implement slow or dangerous methods - which is good.

I just ran into a tricky one from a debugging perspective. Java's List supports a `remove(Object)` method, in addition to `List.remove(index)`. Painless does not. However the error messages are somewhat cryptic and even differ if I try to call `remove` using `params` or not. See this example

```
PUT test/_doc/1
{
"data" : ["1", "2", "3"]
}

GET test/_doc/1

POST test/_update/1
{
"script": {
"source": """
ctx._source.data.remove("2");
""",
"lang": "painless"
}
}
```

The above returns a `wrong_method_type_exception` stating

```
cannot convert MethodHandle(List,int)Object to (Object,String)Object
```

This one has the same intent, but returns a different error message

```
POST test/_update/1
{
"script": {
"source": "ctx._source.data.remove(params.id);",
"lang": "painless",
"params" : {
"id":"2"
}
}
}
```

returns

```
class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap'
```

If you know a little java, you can figure out, that the wrong `remove` method is called resulting in a class cast exception.

Long story short: In addition to unify the error message, we should probably try to have more descriptive error messages stating that this method does not exist or suggest an alternative, especially because `Debug.explain()` returns `ArrayList` despite painless not supporting all methods - which of course is listed in the docs correctly.

Contributor guide

Open the contributing guide

Research direction

Start by reproducing both Painless update requests and compare their errors with the behavior described for Debug.explain(). Read the Painless method-resolution and error-reporting paths, then verify that unsupported methods produce a consistent, descriptive message that identifies the missing method or suggests an alternative.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.