user exceptions to kotlin fixes

This commit is contained in:
sergeych committed 2026-01-08 09:21:25 +01:00
1 parent 1d089db9ff
commit c12804a806
9 files changed
+262 -13

No files matched your search

@@ -0,0 +1,121 @@
/*
* Copyright 2026 Sergey S. Chernov real.sergeych@gmail.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package net.sergeych.lyng
import kotlinx.coroutines.test.runTest
import net.sergeych.lyng.obj.*
import net.sergeych.lynon.lynonDecodeAny
import net.sergeych.lynon.lynonEncodeAny
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertIs
import kotlin.test.assertTrue
class EmbeddingExceptionTest {
@Test
fun testExceptionMessageSerialization() = runTest {
val scope = Script.newScope()
val ex = scope.eval("Exception(\"test message\")")
val encoded = lynonEncodeAny(scope, ex)
val decoded = lynonDecodeAny(scope, encoded)
assertEquals("test message", decoded.getLyngExceptionMessage(scope))
}
@Test
fun testIsInstanceOfString() = runTest {
val scope = Script.newScope()
scope.eval("class T")
val t = scope.eval("T()")
assertTrue(t.isInstanceOf("T"))
assertTrue(t.isInstanceOf("Obj"))
}
@Test
fun testExceptionSerializationAndRethrow() = runTest {
val scope = Script.newScope()
// 1. Define, throw and catch the exception in Lyng to get the object
val errorObj = scope.eval("""
class MyException(val code, m) : Exception(m)
try {
throw MyException(123, "something failed")
} catch {
it
}
""".trimIndent())
assertTrue(errorObj.isLyngException(), "Should be a Lyng exception")
assertTrue(errorObj.isInstanceOf("MyException"), "Should be a MyException")
// 2. Serialize it
val encoded = lynonEncodeAny(scope, errorObj)
// 3. Deserialize it
val decodedObj = lynonDecodeAny(scope, encoded)
// 4. Rethrow it using the new uniform extension
val rethrown = try {
decodedObj.raiseAsExecutionError(scope)
} catch (e: ExecutionError) {
e
}
// 5. Verify the rethrown exception preserves its identity and data
val caughtObj = rethrown.errorObject
assertTrue(caughtObj.isLyngException())
assertTrue(caughtObj.isInstanceOf("MyException"))
assertIs<ObjInstance>(caughtObj)
assertEquals("MyException", caughtObj.objClass.className)
// Verify we can still access the custom fields
// 'code' is a field, so we use readField
val code = caughtObj.readField(scope, "code").value.toKotlin(scope)
assertEquals(123L, code)
val message = caughtObj.getLyngExceptionMessage(scope)
assertEquals("something failed", message)
// Verify stack trace is available
val stack = caughtObj.getLyngExceptionStackTrace(scope)
assertTrue(stack.list.isNotEmpty(), "Stack trace should not be empty")
val errorString = caughtObj.getLyngExceptionString(scope)
assertTrue(errorString.contains("MyException: something failed"), "Error string should contain message")
}
@Test
fun testBuiltInExceptionSerialization() = runTest {
val scope = Script.newScope()
val encoded = try {
scope.eval("throw IllegalArgumentException(\"bad arg\")")
null
} catch (e: ExecutionError) {
lynonEncodeAny(scope, e.errorObject)
}!!
val decodedObj = lynonDecodeAny(scope, encoded)
assertTrue(decodedObj.isLyngException())
assertTrue(decodedObj.isInstanceOf("IllegalArgumentException"))
assertIs<ObjException>(decodedObj)
val message = decodedObj.getLyngExceptionMessage(scope)
assertEquals("bad arg", message)
}
}
@@ -4558,6 +4558,7 @@ class ScriptTest {
val x = try { throw IllegalAccessException("test1") } catch { it }
assertEquals("test1", x.message)
assert( x is IllegalAccessException)
assert( x is Exception )
assertThrows(IllegalAccessException) { throw IllegalAccessException("test2") }
class X : Exception("test3")
@@ -4565,6 +4566,7 @@ class ScriptTest {
println(y)
assertEquals("test3", y.message)
assert( y is X)
assert( y is Exception )
""".trimIndent())
}
@@ -4601,6 +4603,17 @@ class ScriptTest {
assert( x is UserException)
val y = try { throw IllegalStateException() } catch { it }
assert( y is IllegalStateException)
// creating exceptions as usual objects:
val z = IllegalArgumentException()
assert( z is Exception )
assert( z is IllegalArgumentException )
class X : Exception
val t = X()
assert( t is X )
assert( t is Exception )
""".trimIndent())
}
@@ -4635,4 +4648,28 @@ class ScriptTest {
assert(caught.message.contains("Expected DerivedEx, got MyEx"))
""".trimIndent())
}
@Test
fun testRaiseAsError() = runTest {
var x = evalNamed( "tc1","""
IllegalArgumentException("test3")
""".trimIndent())
var x1 = try { x.raiseAsExecutionError() } catch(e: ExecutionError) { e }
println(x1.message)
assertTrue { "tc1:1" in x1.message!! }
assertTrue { "test3" in x1.message!! }
// With user exception classes it should be the same at top level:
x = evalNamed("tc2","""
class E: Exception("test4")
E()
""".trimIndent())
x1 = try { x.raiseAsExecutionError() } catch(e: ExecutionError) { e }
println(x1.message)
assertContains(x1.message!!, "test4")
// the reported error message should include proper trace, which must include
// source name, in our case, is is "tc2":
assertContains(x1.message!!, "tc2")
}
}