Comments (10)
Oh, I just found that the catch branch catches Exception
s but the library throws java.lang.Error
which is another subtype of Throwable
.
I will patch a fix to map js errors to QuickJsException
s so that catch(e: Exception)
will work too.
Thanks for reporting again.
from quickjs-kt.
Thanks!
from quickjs-kt.
The new version is out!
from quickjs-kt.
You are welcome.
And this looks weird, try-catch should work in Kotlin like this test, they are basically the same thing, but yours doesn't work.
I have no idea why Coroutines throws this error, can you provide more code or information code to reproduce it?
from quickjs-kt.
I see, yes the test looks the same to me. Let me see if I can reproduce in a demo app
from quickjs-kt.
That would be nice.
from quickjs-kt.
Here's a JVM example that demonstrates the same issue (but a different stack trace). I would've thought that my try/catch would catch the thrown Error
fun main() {
application {
Window(onCloseRequest = ::exitApplication, title = "jsplayground") {
App()
}
}
}
@Composable
fun App() {
val scope = rememberCoroutineScope()
var result by remember { mutableStateOf("") }
val onRun = fun() {
scope.launch {
try {
quickJs {
val js = """
throw new Error("test")
""".trimIndent()
result = evaluate<Any?>(js).toString()
}
}
catch (e: Exception) {
result = e.toString()
}
}
}
Column {
Button(onRun) {
Text("Click Me")
}
Text(result)
}
}
stack trace:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Error: test
at <eval> (main.js)
at com.dokar.quickjs.QuickJs.getEvaluateResult(Native Method)
at com.dokar.quickjs.QuickJs.evalAndAwait(QuickJs.jni.kt:220)
at com.dokar.quickjs.QuickJs.evaluateInternal(QuickJs.jni.kt:210)
at MainKt$App$onRun$1$1.invokeSuspend(main.kt:73)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2$1.invoke(FlushCoroutineDispatcher.skiko.kt:62)
at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2$1.invoke(FlushCoroutineDispatcher.skiko.kt:57)
at androidx.compose.ui.platform.FlushCoroutineDispatcher.performRun(FlushCoroutineDispatcher.skiko.kt:99)
at androidx.compose.ui.platform.FlushCoroutineDispatcher.access$performRun(FlushCoroutineDispatcher.skiko.kt:37)
at androidx.compose.ui.platform.FlushCoroutineDispatcher$dispatch$2.invokeSuspend(FlushCoroutineDispatcher.skiko.kt:57)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at java.desktop/java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
at java.desktop/java.awt.EventQueue$3.run(Unknown Source)
at java.base/java.security.AccessController.doPrivileged(Unknown Source)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.desktop/java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.desktop/java.awt.EventDispatchThread.run(Unknown Source)
Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [androidx.compose.ui.scene.ComposeContainer$DesktopCoroutineExceptionHandler@63a3cc2b, androidx.compose.runtime.BroadcastFrameClock@79674e78, StandaloneCoroutine{Cancelling}@4fd364bb, FlushCoroutineDispatcher@566badf1]
from quickjs-kt.
For more infomation, if I change this code, the exception is thrown and caught properly:
try {
throw IOException("test fail from kotlin")
// quickJs {
// val js = """
// throw new Error("test")
// """.trimIndent()
//
// result = evaluate<Any?>(js).toString()
// }
}
catch (e: Exception) {
result = e.toString()
}
from quickjs-kt.
The sample code looks good to me, error propagation should work properly in this case. Anyway, will try this code later to see what I can find.
from quickjs-kt.
confirmed, working well for me!
from quickjs-kt.
Related Issues (11)
- Dependency Dashboard
- Kotlin/Native targets
- suspend functions HOT 3
- Setup GitHub CI
- Fix leaks
- `catch()` does not work on Promises that returned by async function bindings
- Proper way to detect circular references
- Repeatable (but not 100% consistent) error in _platform_strcmp HOT 10
- Correct error handling for async functions
- Undefined Symbol for arm64 on linking HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from quickjs-kt.