From b82ffb93c9c296b3df538bfe5e26e585fb45f03b Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sun, 5 Jan 2025 08:16:42 +0100 Subject: [PATCH] [orx-expression-evaluator] Improve exception texts --- .../kotlin/typed/CompiledFunctions.kt | 8 ++++++-- .../commonMain/kotlin/typed/TypedExpressions.kt | 17 +++++++++++------ .../src/commonMain/kotlin/Expressions.kt | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/CompiledFunctions.kt b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/CompiledFunctions.kt index 9950f61b..ce8e6ab6 100644 --- a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/CompiledFunctions.kt +++ b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/CompiledFunctions.kt @@ -28,9 +28,13 @@ fun compileFunction1( return { p0 -> varP0 = p0 - ParseTreeWalker.DEFAULT.walk(listener, root) + try { + ParseTreeWalker.DEFAULT.walk(listener, root) + } catch(e: Throwable) { + throw RuntimeException("Error while evaluating '$expression' with parameter $parameter0=$p0. ${e.message}", e) + } @Suppress("UNCHECKED_CAST") - listener.state.lastExpressionResult as? R ?: error("no result") + listener.state.lastExpressionResult as? R ?: error("No result while evaluating '$expression' with parameter $parameter0=$p0") } } diff --git a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt index bb74a7e0..929718b6 100644 --- a/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt +++ b/orx-expression-evaluator-typed/src/commonMain/kotlin/typed/TypedExpressions.kt @@ -781,7 +781,7 @@ abstract class TypedExpressionListenerBase( IDType.VARIABLE -> s.valueStack.pushChecked( when (name) { "PI" -> PI - else -> constants(name) ?: errorValue("unresolved variable: '${name}'", 0.0 / 0.0) + else -> constants(name) ?: errorValue("unresolved value: '${name}'. Available constant: ${constants}", Unit) } ) @@ -835,10 +835,15 @@ abstract class TypedExpressionListenerBase( is Function<*> -> { + + fun input(): String { + return "in '${node.getParent()?.text}'" + } + @Suppress("UNCHECKED_CAST") - receiver as (String) -> Any + receiver as (String) -> Any? val function = - receiver.invoke(name) + receiver.invoke(name) ?: error("Unresolved function '${name} ${input()}") when (idType) { IDType.MEMBER_FUNCTION0 -> { @@ -849,19 +854,19 @@ abstract class TypedExpressionListenerBase( IDType.MEMBER_FUNCTION1 -> { @Suppress("UNCHECKED_CAST") - function as (Any) -> Any + (function as? (Any) -> Any) ?: error("Cannot cast function '$name' ($function) to (Any) -> Any ${input()}") s.functionStack.push({ x -> function(x[0]) }) } IDType.MEMBER_FUNCTION2 -> { @Suppress("UNCHECKED_CAST") - function as (Any, Any) -> Any + function as? (Any, Any) -> Any ?: error("Cannot cast function '$name' ($function) to (Any, Any) -> Any ${input()}") s.functionStack.push({ x -> function(x[0], x[1]) }) } IDType.MEMBER_FUNCTION3 -> { @Suppress("UNCHECKED_CAST") - function as (Any, Any, Any) -> Any + function as? (Any, Any, Any) -> Any ?: error("Cannot cast function '$name' ($function) to (Any, Any, Any) -> Any ${input()}") s.functionStack.push({ x -> function(x[0], x[1], x[2]) }) } diff --git a/orx-expression-evaluator/src/commonMain/kotlin/Expressions.kt b/orx-expression-evaluator/src/commonMain/kotlin/Expressions.kt index c0ffee82..e5229009 100644 --- a/orx-expression-evaluator/src/commonMain/kotlin/Expressions.kt +++ b/orx-expression-evaluator/src/commonMain/kotlin/Expressions.kt @@ -261,7 +261,7 @@ internal class ExpressionListener( IDType.VARIABLE -> doubleStack.push( when (name) { "PI" -> PI - else -> constants[name] ?: errorValue("unresolved variable: '${name}'", 0.0 / 0.0) + else -> constants[name] ?: errorValue("unresolved value: '${name}'. available values: ${constants}", 0.0 / 0.0) } )