From 30275c7952b9697535da6c7d93a3569aeefe6bd5 Mon Sep 17 00:00:00 2001 From: kjh2064 Date: Wed, 8 Jul 2026 01:54:16 +0900 Subject: [PATCH] Improve admin client error logging --- src/TaxBaik.Web/wwwroot/js/admin-session.js | 52 +++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/src/TaxBaik.Web/wwwroot/js/admin-session.js b/src/TaxBaik.Web/wwwroot/js/admin-session.js index 8017433..879e653 100644 --- a/src/TaxBaik.Web/wwwroot/js/admin-session.js +++ b/src/TaxBaik.Web/wwwroot/js/admin-session.js @@ -46,6 +46,52 @@ window.taxbaikAdminSession = { window.taxbaikAdminSession.postClientLog = postLog; + if (!window._taxbaikConsoleErrorPatched) { + window._taxbaikConsoleErrorPatched = true; + const originalConsoleError = console.error.bind(console); + console.error = function (...args) { + try { + const text = args.map(arg => { + if (arg instanceof Error) { + return `${arg.message}\n${arg.stack || ''}`; + } + if (typeof arg === 'string') return arg; + try { + return JSON.stringify(arg); + } catch { + return String(arg); + } + }).join(' '); + + if (text && !text.includes('taxbaikAdminSession.postClientLog') && !text.includes('client-logs')) { + postLog({ + level: 'error', + source: 'console.error', + message: text.slice(0, 1000), + url: window.location.href, + route: window.location.pathname + window.location.search, + screen: window.taxbaikAdminSession.clientLogState.screen || '', + feature: window.taxbaikAdminSession.clientLogState.feature || '', + action: window.taxbaikAdminSession.clientLogState.action || '', + step: window.taxbaikAdminSession.clientLogState.step || '', + entity: window.taxbaikAdminSession.clientLogState.entity || '', + entityId: window.taxbaikAdminSession.clientLogState.entityId || '', + dataKey: window.taxbaikAdminSession.clientLogState.dataKey || '', + buildVersion: window.taxbaikAdminBuildVersion || '', + component: window.taxbaikAdminComponent || '', + viewportWidth: window.taxbaikAdminSession.getViewportWidth(), + userAgent: navigator.userAgent || '', + stack: args.find(arg => arg instanceof Error)?.stack || '' + }); + } + } catch { + // Logging must never break the UI. + } + + return originalConsoleError(...args); + }; + } + window.addEventListener('error', function (event) { postLog({ level: 'error', @@ -90,6 +136,12 @@ window.taxbaikAdminSession = { stack: reason?.stack || '' }); }); + + window.addEventListener('load', function () { + window.taxbaikAdminSession.traceUiState('startup', 'window.load'); + }, { once: true }); + + window.taxbaikAdminSession.traceUiState('startup', 'initErrorLogging attached'); }, setContext: function (screen, feature, action, step, entity, entityId, dataKey) {