Skip to content

script.model.ScriptInterpreter: emit what cannot be casted to what after a single evaluation of the cast#5329

Open
dilyanpalauzov wants to merge 1 commit intoopenhab:mainfrom
dilyanpalauzov:xbase_cast_error_message
Open

script.model.ScriptInterpreter: emit what cannot be casted to what after a single evaluation of the cast#5329
dilyanpalauzov wants to merge 1 commit intoopenhab:mainfrom
dilyanpalauzov:xbase_cast_error_message

Conversation

@dilyanpalauzov
Copy link
Copy Markdown
Contributor

@dilyanpalauzov dilyanpalauzov commented Feb 6, 2026

Before this change internalEvaluate(castedExpression.getTarget(), context, indicator) was invoked twice: once by super._doEvaluate() and once in the overridden method. When on the first execution an error occured, the second execution was needed to create an error message, stating what cannot be casted to what. However at the time internalEvaluate was called for a second time, internal states have changed, so there might be no more errors with casting, thus producing incorrect error message.

Except the line:

OLD:

throw new ScriptExecutionException(new ScriptError(
   "Could not cast (" + result.class + ")" + result + " to " +typeName,
   castedExpression))

EDIT: the above change was changed, now the only difference is this line

"Could not cast (" + result.class + ")" + result + " to " + typeName

the implementation is the same as in XbaseInterpreter.java, bur rewtiten in Xtend.

The problem has to be addressed in XBase, as it does not provide enough information about what cannot be casted to what — eclipse-xtext/xtext#3595.

I geuess that @Inject CommonTypeComputationServices services is right. I do not understand it, but the super._doEvaluate(XCastedExpression, IEvaluationContext, CancelIndicator) method uses services in the same way.

Closes #5323.

The error messages in the original report contained a string with a date value inside, and it was unclear if the object was a DateTimeType, a StringType or a String object.

15:47:30.403 [ERROR] [l.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'datetime-1' failed: Could not cast 2026-02-02T15:47:30.400405676+0100 to org.openhab.core.library.types.DateTimeType; line 49, column 21, length 35 in datetime
javax.script.ScriptException: Could not cast 2026-02-02T15:47:30.400405676+0100 to org.openhab.core.library.types.DateTimeType; line 49, column 21, length 35 in datetime

The error messages are generated now as:

2026-02-06 11:53:00.580 [ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'dt-1' failed: Could not cast (class org.openhab.core.types.UnDefType)NULL to org.openhab.core.library.types.DateTimeType; line 14, column 20, length 35 in dt

making clear not only the value, which cannot be casted, but also its class.

@dilyanpalauzov
Copy link
Copy Markdown
Contributor Author

When I run locally

$ mvn javadoc:javadoc -pl :org.openhab.core.model.script

it completes without problems.

I do not understand the errors on https://github.com/openhab/openhab-core/actions/runs/21747525180/job/62737006376?pr=5329:

[INFO] --- bnd:7.2.1:bnd-process (default) @ org.openhab.core.config.dispatch ---
Error:  Error fetching link: /home/runner/work/openhab-core/openhab-core/bom/compile/target/site/apidocs. Ignored it.
Error:  Error fetching link: /home/runner/work/openhab-core/openhab-core/bom/test/target/site/apidocs. Ignored it.

[INFO] --- javadoc:3.12.0:javadoc (default-cli) @ org.openhab.core.io.websocket ---
Error:  Error fetching link: /home/runner/work/openhab-core/openhab-core/bom/compile/target/site/apidocs. Ignored it.
Error:  Error fetching link: /home/runner/work/openhab-core/openhab-core/bom/test/target/site/apidocs. Ignored it.
Error:  Error while parsing schema(s).Location [].
com.sun.istack.SAXParseException2: IOException thrown when processing "https://openhab.org/schemas/config-description-1.0.0.xsd". Exception: java.net.UnknownHostException: openhab.org.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  03:03 min (Wall Clock)
[INFO] Finished at: 2026-02-06T10:39:49Z
[INFO] ------------------------------------------------------------------------
Error:  Failed to execute goal org.jvnet.jaxb2.maven2:maven-jaxb2-plugin:0.15.3:generate (generate-jaxb-sources) on project org.openhab.core.addon: Unable to parse input schema(s). Error messages should have been provided. -> [Help 1]
Error:  
Error:  To see the full stack trace of the errors, re-run Maven with the -e switch.
Error:  Re-run Maven using the -X switch to enable full debug logging.
Error:  
Error:  For more information about the errors and possible solutions, please read the following articles:
Error:  [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
Error:  
Error:  After correcting the problems, you can resume the build with the command
Error:    mvn <args> -rf :org.openhab.core.addon
Error: Process completed with exit code 1.

@openhab-bot
Copy link
Copy Markdown
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/issues-with-datetimetype-cast/162281/6

after a single evaluation of the cast.  Before this change
internalEvaluate(castedExpression.getTarget(), context, indicator)
was invoked twice.  When on the first execution an error occured, the
second execution was needed to create an error message, stating what
cannot be casted to what.  However at the time internalEvaluate was
called for a second time, internal states have changed, so there might
be no more errors with casting, thus producing incorrect error message.

Except the line
  "Could not cast (" + result.class + ")" + result + " to " + typeName
the implementation is the same as in XbaseInterpreter.java.
@dilyanpalauzov dilyanpalauzov force-pushed the xbase_cast_error_message branch from f9069c0 to 2d20983 Compare February 18, 2026 13:32
@dilyanpalauzov
Copy link
Copy Markdown
Contributor Author

The other change is switching from

throw new ScriptExecutionException(new ScriptError(

to

throw new EvaluationException(new ClassCastException(

It allows catching ClassCastException within the DSL Script/Rule/Transformation, instead of aborting the XBase interpreter.

First Example

For an item String s with state NULL and the rule:

rule "System started"
when
    System reached start level 100
then
    try {
       s.state as Number
    } catch (ClassCastException e) {
       logError("A", "Caught ClassCastException")
    } finally {
       logError("A", "finally")
    }
    logError("A", "B")
end

does:

  • with throw new EvaluationException(new ClassCastException (…)):
[INFO ] [el.core.internal.ModelRepositoryImpl] - Loading DSL model 'casts.rules'
[ERROR] [org.openhab.core.model.script.A     ] - Caught ClassCastException
[ERROR] [org.openhab.core.model.script.A     ] - finally
[ERROR] [org.openhab.core.model.script.A     ] - B
  • with throw new ScriptExecutionException(new ScriptError(…)):
[INFO ] [el.core.internal.ModelRepositoryImpl] - Loading DSL model 'casts.rules'
[ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'casts-1' failed: Could not cast NULL to java.lang.Number; line 6, column 8, length 17 in casts

Second Example

For an item String s with state NULL:

rule "System started"
when
    System reached start level 100
then
    s.state as Number
    logError("A", "B")
end

does:

  • with throw new EvaluationException(new ClassCastException (…)):
[INFO ] [el.core.internal.ModelRepositoryImpl] - Loading DSL model 'casts2.rules'
[ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'casts2-1' failed: Could not cast (class org.openhab.core.types.UnDefType)NULL to java.lang.Number in casts2
  • with throw new ScriptExecutionException(new ScriptError(…)):
[INFO ] [el.core.internal.ModelRepositoryImpl] - Loading DSL model 'casts2.rules'
[ERROR] [.handler.AbstractScriptModuleHandler] - Script execution of rule with UID 'casts2-1' failed: Could not cast NULL to java.lang.Number; line 5, column 5, length 17 in casts2

The difference is that before the change the line and column numbers were printed.

@openhab-bot
Copy link
Copy Markdown
Collaborator

This pull request has been mentioned on openHAB Community. There might be relevant details there:

https://community.openhab.org/t/major-problems-in-rules-execution-since-openhab5/168397/4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

javax.script.ScriptException: Could not cast DateTimeType

2 participants