ErrorBoundary

fun ErrorBoundary(modifier: Modifier = Modifier, fallback: @Composable BoxScope.(ctx: ErrorBoundaryContext) -> Unit? = null, onError: (err: Throwable) -> Unit? = null, onReset: () -> Unit? = null, state: ErrorBoundaryState = remember { ErrorBoundaryState() }, content: @Composable () -> Unit)

Wrap an ErrorBoundary around other Catch composable functions to catch errors and render a fallback UI.

Note: Typically, this function is defined at the top level of a screen and used for default error handling. Do not propagate errors from ErrorBoundary to higher-level components since the error state is managed by state. Instead, it's recommended to catch and handle domain-specific exceptions within Catch content blocks.

Usage:

ErrorBoundary(
modifier = Modifier.fillMaxSize(),
fallback = {
ContentUnavailable(
error = it.err,
reset = it.reset,
modifier = Modifier.matchParentSize()
)
},
onError = { e -> println(e.toString()) },
onReset = rememberQueriesErrorReset()
) {
Suspense(..) {
val query = rememberGetPostsQuery()
..
Catch(query) { e ->
// You can also write your own error handling logic.
if (e is DomainSpecificException) {
Alert(..)
return@Catch
}
Throw(e)
}
}
}

Parameters

modifier

The modifier to be applied to the layout.

fallback

The fallback UI to render when an error is caught.

onError

The callback to be called when an error is caught.

onReset

The callback to be called when the reset button is clicked.

state

The state of the ErrorBoundary.

content

The content of the ErrorBoundary.

See also