mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-05 16:23:36 +00:00
final consolidation
This commit is contained in:
1 parent
1d05aa10eb
commit
f299c73c4d
2 files changed
+79
-85
No files matched your search
@@ -1,83 +0,0 @@
|
||||
/**
|
||||
* Shared console interception utilities
|
||||
*/
|
||||
|
||||
// List of all console methods to intercept
|
||||
export const CONSOLE_METHODS = [
|
||||
'log', 'warn', 'error', 'info', 'debug',
|
||||
'exception', 'assert', 'clear', 'count', 'countReset',
|
||||
'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd',
|
||||
'table', 'time', 'timeEnd', 'timeLog', 'trace'
|
||||
];
|
||||
|
||||
// Store original console methods
|
||||
export function storeOriginalConsole() {
|
||||
const original = {};
|
||||
CONSOLE_METHODS.forEach(method => {
|
||||
if (typeof console[method] === 'function') {
|
||||
original[method] = console[method];
|
||||
}
|
||||
});
|
||||
return original;
|
||||
}
|
||||
|
||||
// Override console methods with custom handler
|
||||
export function overrideConsoleMethods(originalConsole, messageHandler) {
|
||||
CONSOLE_METHODS.forEach(method => {
|
||||
if (typeof console[method] === 'function') {
|
||||
console[method] = (...args) => {
|
||||
// Call original method first
|
||||
try {
|
||||
originalConsole[method](...args);
|
||||
} catch (e) {
|
||||
// If original fails, at least send to parent
|
||||
messageHandler(method, ...args);
|
||||
return;
|
||||
}
|
||||
|
||||
// Send to parent
|
||||
messageHandler(method, ...args);
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set up global error handlers
|
||||
export function setupGlobalErrorHandlers(messageHandler) {
|
||||
// Override window.onerror to catch unhandled errors
|
||||
const originalOnError = window.onerror;
|
||||
window.onerror = (message, source, lineno, colno, error) => {
|
||||
// Call original handler first
|
||||
if (originalOnError) {
|
||||
originalOnError(message, source, lineno, colno, error);
|
||||
}
|
||||
|
||||
// Send to parent
|
||||
messageHandler('error', `Uncaught Error: ${message}`, {
|
||||
source,
|
||||
lineno,
|
||||
colno,
|
||||
error: error ? error.message : 'No error object',
|
||||
stack: error ? error.stack : 'No stack trace'
|
||||
});
|
||||
|
||||
return false; // Don't prevent default
|
||||
};
|
||||
|
||||
// Override window.onunhandledrejection for Promise rejections
|
||||
const originalOnUnhandledRejection = window.onunhandledrejection;
|
||||
window.onunhandledrejection = (event) => {
|
||||
// Call original handler first
|
||||
if (originalOnUnhandledRejection) {
|
||||
originalOnUnhandledRejection(event);
|
||||
}
|
||||
|
||||
// Send to parent
|
||||
messageHandler('error', 'Unhandled Promise Rejection', {
|
||||
reason: event.reason,
|
||||
promise: event.promise ? 'Promise object' : 'No promise object'
|
||||
});
|
||||
|
||||
event.preventDefault(); // Prevent default console warning
|
||||
};
|
||||
}
|
||||
@@ -3,8 +3,85 @@
|
||||
* Combines console interception and client initialization
|
||||
*/
|
||||
|
||||
// Import console interception utilities
|
||||
import { storeOriginalConsole, overrideConsoleMethods, setupGlobalErrorHandlers, CONSOLE_METHODS } from './console-utils.js';
|
||||
// Console interception utilities (merged from console-utils.js)
|
||||
const CONSOLE_METHODS = [
|
||||
'log', 'warn', 'error', 'info', 'debug',
|
||||
'exception', 'assert', 'clear', 'count', 'countReset',
|
||||
'dir', 'dirxml', 'group', 'groupCollapsed', 'groupEnd',
|
||||
'table', 'time', 'timeEnd', 'timeLog', 'trace'
|
||||
];
|
||||
|
||||
// Store original console methods
|
||||
function storeOriginalConsole() {
|
||||
const original = {};
|
||||
CONSOLE_METHODS.forEach(method => {
|
||||
if (typeof console[method] === 'function') {
|
||||
original[method] = console[method];
|
||||
}
|
||||
});
|
||||
return original;
|
||||
}
|
||||
|
||||
// Override console methods with custom handler
|
||||
function overrideConsoleMethods(originalConsole, messageHandler) {
|
||||
CONSOLE_METHODS.forEach(method => {
|
||||
if (typeof console[method] === 'function') {
|
||||
console[method] = (...args) => {
|
||||
// Call original method first
|
||||
try {
|
||||
originalConsole[method](...args);
|
||||
} catch (e) {
|
||||
// If original fails, at least send to parent
|
||||
messageHandler(method, ...args);
|
||||
return;
|
||||
}
|
||||
|
||||
// Send to parent
|
||||
messageHandler(method, ...args);
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Set up global error handlers
|
||||
function setupGlobalErrorHandlers(messageHandler) {
|
||||
// Override window.onerror to catch unhandled errors
|
||||
const originalOnError = window.onerror;
|
||||
window.onerror = (message, source, lineno, colno, error) => {
|
||||
// Call original handler first
|
||||
if (originalOnError) {
|
||||
originalOnError(message, source, lineno, colno, error);
|
||||
}
|
||||
|
||||
// Send to parent
|
||||
messageHandler('error', `Uncaught Error: ${message}`, {
|
||||
source,
|
||||
lineno,
|
||||
colno,
|
||||
error: error ? error.message : 'No error object',
|
||||
stack: error ? error.stack : 'No stack trace'
|
||||
});
|
||||
|
||||
return false; // Don't prevent default
|
||||
};
|
||||
|
||||
// Override window.onunhandledrejection for Promise rejections
|
||||
const originalOnUnhandledRejection = window.onunhandledrejection;
|
||||
window.onunhandledrejection = (event) => {
|
||||
// Call original handler first
|
||||
if (originalOnUnhandledRejection) {
|
||||
originalOnUnhandledRejection(event);
|
||||
}
|
||||
|
||||
// Send to parent
|
||||
messageHandler('error', 'Unhandled Promise Rejection', {
|
||||
reason: event.reason,
|
||||
promise: event.promise ? 'Promise object' : 'No promise object'
|
||||
});
|
||||
|
||||
event.preventDefault(); // Prevent default console warning
|
||||
};
|
||||
}
|
||||
|
||||
// Early Console Interception - runs immediately
|
||||
class EarlyConsoleInterceptor {
|
||||
|
||||
Reference in new issue
Block a user