UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

68 lines 2.42 kB
/** * @module Resilience */ import {} from "../../../execution-context/contracts/_module.js"; import {} from "../../../middleware/contracts/_module.js"; import { callErrorPolicyOnValue, resolveAsyncLazyable, callInvokable, callErrorPolicyOnThrow, } from "../../../utilities/_module.js"; /** * IMPORT_PATH: `"@daiso-tech/core/resilience"` * @group Middlewares */ export function fallback(settings) { const { fallbackValue, errorPolicy, onFallback = () => { } } = settings; return async ({ args, next, context }) => { try { const value = await next(); if (!callErrorPolicyOnValue(errorPolicy, value)) { return value; } const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue); try { void (async () => { try { await callInvokable(onFallback, { error: value, fallbackValue: resolvedFallbackValue, args, context, }); } catch (error) { console.error("Error occurred in onFallback callback:", error); } })(); } catch { /* EMPTY */ } return resolvedFallbackValue; // Handle fallback value if an error is thrown } catch (error) { if (!(await callErrorPolicyOnThrow(errorPolicy, error))) { throw error; } const resolvedFallbackValue = await resolveAsyncLazyable(fallbackValue); try { void (async () => { try { await callInvokable(onFallback, { error, fallbackValue: resolvedFallbackValue, args, context, }); } catch (error_) { console.error("Error occurred in onFallback callback:", error_); } })(); } catch { /* EMPTY */ } return resolvedFallbackValue; } }; } //# sourceMappingURL=fallback.js.map