/** * Safe Wrapper Utilities * * Error isolation utilities to prevent feature errors from crashing the SDK. * Following PostHog's safewrap pattern. * * All errors are routed through the central level-gated logger * (`getLogger()`) so they respect the integrator's `log_level` choice. * `error`-level messages always surface in the default `warn` floor. */ /** * Logger interface for error reporting. Kept narrow so callers can inject a * custom logger in tests without depending on the full `Logger` shape. */ interface Logger { error: (...args: unknown[]) => void; warn: (...args: unknown[]) => void; } /** * Wrap a function to catch and log errors without throwing. * Prevents errors in one feature from crashing the entire SDK. * * @param fn - The function to wrap * @param context - Optional context string for error messages * @param logger - Optional custom logger * @returns Wrapped function that catches errors * * @example * ```typescript * const safeHandler = safewrap( * (event: Event) => this._handleEvent(event), * 'Autocapture._handleEvent' * ); * document.addEventListener('click', safeHandler); * ``` */ export declare function safewrap any>(fn: F, context?: string, logger?: Logger): F; /** * Try to execute a function and return undefined on error. * Useful for one-off operations that might fail. * * @param fn - The function to try * @param fallback - Optional fallback value on error * @returns The result or fallback/undefined * * @example * ```typescript * const value = trySafe(() => JSON.parse(data), {}); * const element = trySafe(() => document.querySelector('.my-class')); * ``` */ export declare function trySafe(fn: () => T): T | undefined; export declare function trySafe(fn: () => T, fallback: F): T | F; /** * Wrap all methods of a class prototype with safewrap. * Useful for wrapping entire feature classes. * * @param Class - The class constructor * @param methodNames - Array of method names to wrap * @param logger - Optional custom logger * * @example * ```typescript * class MyFeature { * capture() { ... } * identify() { ... } * } * safewrapClass(MyFeature, ['capture', 'identify']); * ``` */ export declare function safewrapClass any>(Class: T, methodNames: (keyof InstanceType)[], logger?: Logger): void; /** * Create a safe event handler that won't break on errors. * Specifically designed for DOM event handlers. * * @param handler - The event handler function * @param context - Context string for error messages * @returns Safe event handler * * @example * ```typescript * document.addEventListener('click', safeEventHandler( * (e) => this._onClick(e), * 'Autocapture.onClick' * )); * ``` */ export declare function safeEventHandler(handler: (event: E) => void, context?: string): (event: E) => void; /** * Execute an async function safely, catching any errors. * * @param fn - The async function to execute * @param context - Context string for error messages * @param logger - Optional custom logger * @returns Promise that resolves to result or undefined * * @example * ```typescript * const result = await trySafeAsync( * () => fetch('/api/data'), * 'fetchData' * ); * ``` */ export declare function trySafeAsync(fn: () => Promise, context?: string, logger?: Logger): Promise; export {};