Skip to content

safeCall

safeCall<T>(fn, fallback, debug?, label?): Promise<T>

Defined in: src/policies/sdk/helpers.ts:107

Execute an async operation with graceful error handling.

Designed for store-backed policies (cache, rate-limit, circuit-breaker) where a store failure should degrade gracefully - not crash the request. Returns the fallback value if fn throws.

T

() => Promise<T>

The async operation to attempt.

T

Value to return if fn throws.

DebugLogger

Optional debug logger for error reporting.

string

Optional label for the debug message (e.g. "store.get()").

Promise<T>

The result of fn, or fallback on error.

const cached = await safeCall(
() => store.get(key),
null,
debug,
"store.get()",
);