Per Jansson
03/29/2023, 6:21 AM@Composable
fun MyApp() {
..
..
ThirdPartyComposableThatSometimesErrorsOut() {
...
}
}
I can’t do
@Composable
fun MyApp() {
..
..
try {
ThirdPartyComposableThatSometimesErrorsOut() {
...
}
} catch (error: Throwable) {
// Handle the error here
}
}
mgrazianodecastro
03/30/2023, 4:01 AMsealed class MyErrorHandler {
data class Ok(val content: @Composable () -> Unit): MyErrorHandler()
object Err: MyErrorHandler()
}
fun getMyThirdPartyComposable(): MyErrorHandler {
return try {
MyErrorHandler.Ok {
ComposeView(this).apply { // get the context from somewhere
setContent {
Text(text = "Hello world.")
}
}
}
}catch (err: Exception){
MyErrorHandler.Err
}
}
}
mgrazianodecastro
03/30/2023, 4:02 AMPer Jansson
03/30/2023, 9:44 AMMyErrorHandler.Ok
2. When I’m using it to compose the 3rd party composable when it works, it returns MyErrorHandler.Ok
but does not compose anything on the screen.
I’m passing in LocalContext.current
as context
and rendering the response from getMyThirdPartyComposable
with something like this:
@Composable
fun MyView() {
val context = LocalContext.current
val errorHandler = getMyThirdPartyComposable(context)
when (errorHandler) {
is MyErrorHandler.Ok -> {
errorHandler.content()
}
is MyErrorHandler.Err -> {
// handle the error
}
}
}