@embrace-io/web-sdk
Version:
33 lines (32 loc) • 1.3 kB
JavaScript
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
//#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
exports.createSafeProxy = createSafeProxy;
//# sourceMappingURL=createSafeProxy.cjs.map