more tools to deal with Lyng exeption in Kotlin
This commit is contained in:
1 parent
c12804a806
commit
2acb60697d
6 files changed
+53
-5
No files matched your search
@@ -337,8 +337,37 @@ fun Obj.isLyngException(): Boolean = isInstanceOf("Exception")
|
||||
/**
|
||||
* Get the exception message.
|
||||
*/
|
||||
suspend fun Obj.getLyngExceptionMessage(scope: Scope): String =
|
||||
invokeInstanceMethod(scope, "message").toString(scope).value
|
||||
suspend fun Obj.getLyngExceptionMessage(scope: Scope?=null): String {
|
||||
require( this.isLyngException() )
|
||||
val s = scope ?: Script.newScope()
|
||||
return invokeInstanceMethod(s, "message").toString(s).value
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a detailed exception message including the stack trace for a Lyng exception.
|
||||
* This function is designed to handle objects identified as Lyng exceptions.
|
||||
*
|
||||
* @param scope the scope to be used for fetching the exception message and stack trace.
|
||||
* If null, a new scope will be created.
|
||||
* @return a string combining the exception message, the location ("at"),
|
||||
* and the formatted stack trace information.
|
||||
* The stack trace details each frame using indentation for clarity.
|
||||
* @throws IllegalArgumentException if the object is not a Lyng exception.
|
||||
*/
|
||||
suspend fun Obj.getLyngExceptionMessageWithStackTrace(scope: Scope?=null): String {
|
||||
require( this.isLyngException() )
|
||||
val s = scope ?: Script.newScope()
|
||||
val msg = getLyngExceptionMessage(s)
|
||||
val trace = getLyngExceptionStackTrace(s)
|
||||
var at = "unknown"
|
||||
val stack = if( !trace.list.isEmpty() ) {
|
||||
val first = trace.list[0]
|
||||
at = (first.readField(s, "at").value as ObjString).value
|
||||
"\n" + trace.list.map { " at " + it.toString(s).value }.joinToString("\n")
|
||||
}
|
||||
else ""
|
||||
return "$at: $msg$stack"
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the exception stack trace.
|
||||
|
||||
@@ -4671,5 +4671,17 @@ class ScriptTest {
|
||||
// source name, in our case, is is "tc2":
|
||||
assertContains(x1.message!!, "tc2")
|
||||
}
|
||||
@Test
|
||||
fun testLyngToKotlinExceptionHelpers() = runTest {
|
||||
var x = evalNamed( "tc1","""
|
||||
IllegalArgumentException("test3")
|
||||
""".trimIndent())
|
||||
assertEquals("""
|
||||
tc1:1:1: test3
|
||||
at tc1:1:1: IllegalArgumentException("test3")
|
||||
""".trimIndent(),
|
||||
x.getLyngExceptionMessageWithStackTrace()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user