https://kotlinlang.org logo
Title
j

Jilles van Gurp

12/15/2020, 9:44 AM
I'm trying to integrate our mpp client library into a fritz2 project. We have a login function that suspends that we are trying to call from a handler on the login form. When it runs, we get this error.
{…}

"cause_us9j0c$_0": TypeError: NetworkError when attempting to fetch resource.

"message_q7r8iu$_0": "Fail to fetch"

name: "Error"

stack: "captureStack@webpack-internal:///./kotlin-dce-dev/kotlin.js:39116:27\nError_0@webpack-internal:///./kotlin-dce-dev/kotlin.js:39410:14\ncommonFetch$lambda$lambda_1/<@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:17565:68\npromise callback*commonFetch$lambda/<@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:17581:11\nsuspendCancellableCoroutine$lambda_1/<@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:17547:16\nCoroutine$commonFetch.prototype.doResume@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:17602:116\ncommonFetch@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:17630:23\nCoroutine$execute_dkgphz$.prototype.doResume@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:16974:25\nJsClientEngine.prototype.execute_dkgphz$@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:17004:21\nCoroutine$HttpClientEngine$executeWithinCallContext$lambda.prototype.doResume@webpack-internal:///./kotlin-dce-dev/ktor-ktor-client-core-jsLegacy.js:1591:58\nCoroutineImpl.prototype.resumeWith_tl1gpc$@webpack-internal:///./kotlin-dce-dev/kotlin.js:39279:35\nDispatchedTask.prototype.run@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:34823:22\nMessageQueue.prototype.process@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:38215:15\nWindowMessageQueue$schedule$lambda/<@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:38170:27\npromise callback*WindowMessageQueue.prototype.schedule@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:38175:25\nMessageQueue.prototype.enqueue_771g0p$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:38202:10\nWindowDispatcher.prototype.dispatch_5bn72i$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:38126:16\nJsMainDispatcher.prototype.dispatch_5bn72i$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:37913:17\ndispatch@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:34864:20\nCancellableContinuationImpl.prototype.dispatchResume_0@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:1549:11\nCancellableContinuationImpl.prototype.completeResume_za3rmp$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:1647:8\nAbstractChannel$ReceiveElement.prototype.completeResumeReceive_11rb$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:6207:13\nArrayChannel.prototype.offerInternal_11rb$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:6938:28\nAbstractSendChannel.prototype.offer_11rb$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:5316:21\nChannelCoroutine.prototype.offer_11rb$@webpack-internal:///./kotlin-dce-dev/kotlinx-coroutines-core.js:7768:26\nWithEvents$subscribe$lambda$lambda/<@webpack-internal:///./kotlin-dce-dev/fritz2-core.js:3398:13\n"

<prototype>: Object { constructor: Error_0(), stack: "", … }
kotlinx-coroutines-core.js:37890
I've tried calling the function from our main function like this and that works. Doing the same in the handler doesn't seem to work. It looks like we're hitting this issue: https://youtrack.jetbrains.com/issue/KT-32708 I would appreciate any suggested workarounds.
GlobalScope.launch {
        try {
            val res = service.login("<mailto:jvg@tryformation.com|jvg@tryformation.com>","xxxx")
            println(res)
        } catch (e: Exception) {
            println(":-(")
        }
    }
Also tried the GlobalScope.promise but always get the same error when it runs from the fritz2 handler
To be clear the mpp client uses ktor-client & kotlinx serialization. We use it on Android as well. We enabled CORS headers on our server and the login function works fine when called outside the fritz2 handler. The bug suggests this is some kind of compiler issue.
h

HankG

12/16/2020, 6:31 PM
This sort of merged workflow with KMM/KMP is something I'm interested in trying...curious to see what the resolution is
j

Jilles van Gurp

12/17/2020, 9:18 AM
@Jacob Otto from our side managed to work around it; can you maybe share what you did here?
j

Jacob Otto

12/17/2020, 10:09 AM
Unfortunately I did not get around this one. But I only get it in the firefox browser and only on the first login attempt after compiling.
j

Jilles van Gurp

12/17/2020, 4:28 PM
mystery solved & pilot error; we were using a form tag which caused the page to reload and login at the same time. Sometimes it would cancel the co-routine and trigger the ugly error.
solution, get rid of the form tag and it started working nicely.