GithubHelp home page GithubHelp logo

Exception handling about quickjs-kt HOT 10 CLOSED

dgmltn avatar dgmltn commented on May 31, 2024
Exception handling

from quickjs-kt.

Comments (10)

dokar3 avatar dokar3 commented on May 31, 2024 1

Oh, I just found that the catch branch catches Exceptions but the library throws java.lang.Error which is another subtype of Throwable.

I will patch a fix to map js errors to QuickJsExceptions so that catch(e: Exception) will work too.

Thanks for reporting again.

from quickjs-kt.

dgmltn avatar dgmltn commented on May 31, 2024 1

Thanks!

from quickjs-kt.

dokar3 avatar dokar3 commented on May 31, 2024 1

The new version is out!

from quickjs-kt.

dokar3 avatar dokar3 commented on May 31, 2024

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.

dgmltn avatar dgmltn commented on May 31, 2024

I see, yes the test looks the same to me. Let me see if I can reproduce in a demo app

from quickjs-kt.

dokar3 avatar dokar3 commented on May 31, 2024

That would be nice.

from quickjs-kt.

dgmltn avatar dgmltn commented on May 31, 2024

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.

dgmltn avatar dgmltn commented on May 31, 2024

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.

dokar3 avatar dokar3 commented on May 31, 2024

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.

dgmltn avatar dgmltn commented on May 31, 2024

confirmed, working well for me!

from quickjs-kt.

Related Issues (11)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.