UNPKG

@embrace-io/web-sdk

Version:
32 lines (31 loc) 1.21 kB
//#region src/utils/createSafeProxy/createSafeProxy.ts /** * Creates a proxy that wraps all method calls in try-catch. * On error, logs via diag and returns the NoOp manager's value. * * @param target - The object to wrap (typically a ProxyManager) * @param noOpFallback - A NoOp implementation to get fallback return values * @param diag - Logger for error reporting * @param excludeMethods - Methods to exclude from wrapping (e.g., internal methods) */ function createSafeProxy(target, noOpFallback, diag, excludeMethods = /* @__PURE__ */ new Set()) { return new Proxy(target, { get(obj, prop, receiver) { const value = Reflect.get(obj, prop, receiver); if (typeof value !== "function") return value; if (excludeMethods.has(prop)) return value; return (...args) => { try { return value.apply(obj, args); } catch (e) { const message = e instanceof Error ? e.message : "Unknown error"; diag.error(`${String(prop)}: ${message}`); const fallbackMethod = noOpFallback[prop]; if (typeof fallbackMethod === "function") return fallbackMethod.apply(noOpFallback, args); return; } }; } }); } //#endregion export { createSafeProxy }; //# sourceMappingURL=createSafeProxy.js.map