| File was renamed from assets/js/dash/ErrorHandler.js |
| | |
| | | return defaultMessages[type] || defaultMessages.unknown; |
| | | } |
| | | |
| | | /** |
| | | * Log error to server |
| | | */ |
| | | async logErrorToServer(type, message, context) { |
| | | try { |
| | | if (!this.options.apiUrl) return; |
| | | /** |
| | | * Log error to server with enhanced context |
| | | */ |
| | | async logErrorToServer(type, message, context) { |
| | | try { |
| | | if (!this.options.apiUrl) return; |
| | | |
| | | const data = new FormData(); |
| | | data.append('error_type', type); |
| | | data.append('message', message); |
| | | data.append('context', JSON.stringify({ |
| | | ...context, |
| | | url: window.location.href, |
| | | userAgent: navigator.userAgent, |
| | | timestamp: new Date().toISOString() |
| | | })); |
| | | // Enhanced context with component tracking |
| | | const enhancedContext = { |
| | | ...context, |
| | | url: window.location.href, |
| | | pathname: window.location.pathname, |
| | | userAgent: navigator.userAgent, |
| | | timestamp: new Date().toISOString(), |
| | | viewport: `${window.innerWidth}x${window.innerHeight}`, |
| | | component: context.component || this.extractComponentFromStack(context.stack), |
| | | method: context.method || this.extractMethodFromStack(context.stack), |
| | | stack: context.stack || (context.error?.stack), |
| | | isLoggedIn: window.auth.isAuthenticated(), |
| | | source: 'frontend' |
| | | }; |
| | | |
| | | // Use fetch with no-cors to ensure this always succeeds |
| | | // even if there are CORS issues |
| | | await fetch(`${this.options.apiUrl}errors/log`, { |
| | | method: 'POST', |
| | | headers: { |
| | | 'X-WP-Nonce': window.feedSettings?.nonce || '' |
| | | }, |
| | | body: data |
| | | }); |
| | | } catch (e) { |
| | | // Silently fail - we don't want errors in error reporting |
| | | console.warn('Failed to log error to server', e); |
| | | } |
| | | } |
| | | const data = new FormData(); |
| | | data.append('error_type', type); |
| | | data.append('message', message); |
| | | data.append('context', JSON.stringify(enhancedContext)); |
| | | |
| | | await fetch(`${this.options.apiUrl}errors/log`, { |
| | | method: 'POST', |
| | | headers: { |
| | | 'X-WP-Nonce': window.auth.getNonce() |
| | | }, |
| | | body: data |
| | | }); |
| | | } catch (e) { |
| | | console.warn('Failed to log error to server', e); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * Extract component name from error stack |
| | | */ |
| | | extractComponentFromStack(stack) { |
| | | if (!stack) return 'Unknown'; |
| | | |
| | | // Try to extract class/component name from stack trace |
| | | const match = stack.match(/at\s+(\w+)\./); |
| | | return match ? match[1] : 'Unknown'; |
| | | } |
| | | |
| | | /** |
| | | * Extract method name from error stack |
| | | */ |
| | | extractMethodFromStack(stack) { |
| | | if (!stack) return null; |
| | | |
| | | // Try to extract method name |
| | | const match = stack.match(/at\s+\w+\.(\w+)\s+/); |
| | | return match ? match[1] : null; |
| | | } |
| | | |
| | | /** |
| | | * Display error notification |
| | |
| | | */ |
| | | handleAuthError() { |
| | | // Redirect to login page if user isn't logged in |
| | | if (window.feedSettings && window.feedSettings.loginUrl) { |
| | | window.location.href = window.feedSettings.loginUrl; |
| | | if (window.jvbSettings && window.jvbSettings.loginUrl) { |
| | | window.location.href = window.jvbSettings.loginUrl; |
| | | return; |
| | | } |
| | | |
| | |
| | | }); |
| | | } |
| | | } |
| | | document.addEventListener('DOMContentLoaded', function () { |
| | | window.jvbError = new ErrorHandler({ |
| | | api: jvbSettings.api, |
| | | logToServer: true, |
| | | displayNotifications: true, |
| | | notificationDuration: 5000, |
| | | retryEnabled: true, |
| | | maxRetries: 3 |
| | | document.addEventListener('DOMContentLoaded', async function () { |
| | | window.auth.subscribe((event) => { |
| | | if (event === 'auth-loaded') { |
| | | window.jvbError = new ErrorHandler({ |
| | | api: jvbSettings.api, |
| | | logToServer: true, |
| | | displayNotifications: true, |
| | | notificationDuration: 5000, |
| | | retryEnabled: true, |
| | | maxRetries: 3 |
| | | }); |
| | | } |
| | | }); |
| | | |
| | | }); |
| | | |