FallbackPolicy

class FallbackPolicy(config: FallbackConfig<Any?>, onFallbackTriggered: (Throwable) -> Unit = {})

A resilience policy that provides a fallback mechanism.

This policy executes a given code block and, if an exception (a Throwable) is caught, it executes a predefined fallback function. The result of the fallback function is then returned as the result of the execution.

This is useful for providing default values or alternative logic when an operation fails, ensuring that the application can continue to function gracefully.

Parameters

config

The configuration for the fallback policy, specifying the action to take on failure.

onFallbackTriggered

Optional callback invoked when fallback is used (e.g. for telemetry).

Constructors

Link copied to clipboard
constructor(config: FallbackConfig<Any?>, onFallbackTriggered: (Throwable) -> Unit = {})

Functions

Link copied to clipboard
suspend fun <T> execute(block: suspend () -> T): T

Executes block; on failure (excluding kotlinx.coroutines.CancellationException) invokes FallbackConfig.onFallback and returns its result.