Skip to content

Commit

Permalink
Install a global uncaught exception handler (#1735)
Browse files Browse the repository at this point in the history
* Install a global uncaught exception handler

* Reference a coroutines feature request

Kotlin/kotlinx.coroutines#3978
  • Loading branch information
squarejesse authored Dec 8, 2023
1 parent f275f2a commit 5815ba4
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 0 deletions.
12 changes: 12 additions & 0 deletions redwood-treehouse-guest/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ kotlin {
api projects.redwoodTreehouseGuestCompose
}
}
nonJsMain {
dependsOn(commonMain)
}
androidMain {
dependsOn(nonJsMain)
}
jvmMain {
dependsOn(nonJsMain)
}
nativeMain {
dependsOn(nonJsMain)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* 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 app.cash.redwood.treehouse

import kotlinx.coroutines.CoroutineExceptionHandler

/**
* Customize the runtime for this Treehouse application.
*/
internal expect fun prepareEnvironment(
coroutineExceptionHandler: CoroutineExceptionHandler,
)
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class StandardAppLifecycle(
internal val json: Json,
internal val widgetVersion: UInt,
) : AppLifecycle {
private var started = false
private lateinit var host: Host

private lateinit var broadcastFrameClock: BroadcastFrameClock
Expand Down Expand Up @@ -60,9 +61,14 @@ public class StandardAppLifecycle(
internal val coroutineScope = CoroutineScope(coroutineExceptionHandler)

override fun start(host: Host) {
check(!started) { "already started" }
this.started = true

this.host = host
this.broadcastFrameClock = BroadcastFrameClock { host.requestFrame() }
this.frameClock = broadcastFrameClock

prepareEnvironment(coroutineExceptionHandler)
}

override fun sendFrame(timeNanos: Long) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* 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 app.cash.redwood.treehouse

import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineExceptionHandler

/**
* This installs a global coroutine exception handler using an internal API.
*
* This is necessary for uncaught exceptions to be delivered to the host application. Otherwise
* they're logged to the console where they're easily missed.
*
* This assumes we're the [StandardAppLifecycle] owns the entire JS runtime.
*/
@Suppress("INVISIBLE_MEMBER") // https://github.com/Kotlin/kotlinx.coroutines/issues/3978
internal actual fun prepareEnvironment(
coroutineExceptionHandler: CoroutineExceptionHandler,
) {
kotlinx.coroutines.internal.ensurePlatformExceptionHandlerLoaded(
object : CoroutineExceptionHandler by coroutineExceptionHandler {
override fun handleException(context: CoroutineContext, exception: Throwable) {
coroutineExceptionHandler.handleException(context, exception)
throw kotlinx.coroutines.internal.ExceptionSuccessfullyProcessed
}
},
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2023 Square, Inc.
*
* 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 app.cash.redwood.treehouse

import kotlinx.coroutines.CoroutineExceptionHandler

internal actual fun prepareEnvironment(
coroutineExceptionHandler: CoroutineExceptionHandler,
) {
// Do nothing.
}

0 comments on commit 5815ba4

Please sign in to comment.