import { browserLog } from '@/utils/browserLog'; // Global error handler for chart disposal errors export function setupChartErrorHandler() { // pagehide works with back/forward caching and documents that disallow unload. // Restore normal reporting if the browser later restores this same document. let isUnloading = false; window.addEventListener('pagehide', () => { isUnloading = true; }); window.addEventListener('pageshow', () => { isUnloading = false; }); // Intercept console errors for chart disposal const originalError = browserLog.error; browserLog.error = (...args) => { // Check if this is a chart disposal error during unload if (isUnloading) { const errorStr = args.join(' '); if ( errorStr.includes('Object is disposed') || errorStr.includes('Cannot read properties of null') || errorStr.includes('lightweight-charts') ) { // Suppress the error during unload return; } } // Otherwise, log normally originalError.apply(console, args); }; // Also catch unhandled errors window.addEventListener('error', event => { if (isUnloading && event.error?.message?.includes('Object is disposed')) { event.preventDefault(); return; } }); // Catch unhandled promise rejections window.addEventListener('unhandledrejection', event => { if (isUnloading && event.reason?.message?.includes('Object is disposed')) { event.preventDefault(); return; } }); }