logging
This commit is contained in:
@@ -112,6 +112,18 @@ export class GlobalErrorHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this is a network/CORS error (expected in some cases)
|
||||
if (error instanceof TypeError && error.message.includes('fetch')) {
|
||||
this.logger.warn('Network error detected', {
|
||||
type: 'network_error',
|
||||
message: error.message,
|
||||
url: event.filename,
|
||||
line: event.lineno,
|
||||
col: event.colno,
|
||||
});
|
||||
return; // Don't prevent default for network errors
|
||||
}
|
||||
|
||||
const enhancedContext = this.captureEnhancedContext('window_error', {
|
||||
filename: event.filename,
|
||||
lineno: event.lineno,
|
||||
@@ -119,14 +131,13 @@ export class GlobalErrorHandler {
|
||||
message: event.message,
|
||||
});
|
||||
|
||||
// Log with maximum detail
|
||||
// Log with appropriate detail
|
||||
this.logErrorWithMaximumDetail(error, enhancedContext);
|
||||
|
||||
// Store in history
|
||||
this.addToHistory(error, enhancedContext);
|
||||
|
||||
|
||||
// Report to external if enabled
|
||||
// Report to external if enabled (but not for network errors)
|
||||
if (this.options.reportToExternal) {
|
||||
this.reportToExternal(error, enhancedContext);
|
||||
}
|
||||
@@ -154,19 +165,27 @@ export class GlobalErrorHandler {
|
||||
return;
|
||||
}
|
||||
|
||||
// Check if this is a network error (expected)
|
||||
if (error instanceof TypeError && error.message.includes('fetch')) {
|
||||
this.logger.warn('Unhandled promise rejection - network error', {
|
||||
type: 'network_error',
|
||||
message: error.message,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const enhancedContext = this.captureEnhancedContext('unhandled_promise', {
|
||||
promise: event.promise,
|
||||
reason: typeof error === 'string' ? error : error?.message || 'Unknown promise rejection',
|
||||
});
|
||||
|
||||
// Log with maximum detail
|
||||
// Log with appropriate detail
|
||||
this.logErrorWithMaximumDetail(error, enhancedContext);
|
||||
|
||||
// Store in history
|
||||
this.addToHistory(error, enhancedContext);
|
||||
|
||||
|
||||
// Report to external if enabled
|
||||
// Report to external if enabled (but not for network errors)
|
||||
if (this.options.reportToExternal) {
|
||||
this.reportToExternal(error, enhancedContext);
|
||||
}
|
||||
@@ -284,74 +303,44 @@ export class GlobalErrorHandler {
|
||||
}
|
||||
|
||||
/**
|
||||
* Log error with maximum detail
|
||||
* Log error with appropriate detail
|
||||
*/
|
||||
private logErrorWithMaximumDetail(error: Error | ApiError, context: Record<string, unknown>): void {
|
||||
if (!this.options.verboseLogging) return;
|
||||
|
||||
const isApiError = error instanceof ApiError;
|
||||
|
||||
// Group all related information
|
||||
console.groupCollapsed(`%c[GLOBAL ERROR] ${error.name || 'Error'}: ${error.message}`,
|
||||
'color: #ff4444; font-weight: bold; font-size: 14px;'
|
||||
);
|
||||
const isWarning = isApiError && error.getSeverity() === 'warn';
|
||||
|
||||
// Error details
|
||||
console.log('Error Details:', {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
stack: error.stack,
|
||||
type: isApiError ? error.type : 'N/A',
|
||||
severity: isApiError ? error.getSeverity() : 'error',
|
||||
retryable: isApiError ? error.isRetryable() : 'N/A',
|
||||
connectivity: isApiError ? error.isConnectivityIssue() : 'N/A',
|
||||
});
|
||||
|
||||
// Context information
|
||||
console.log('Context:', context);
|
||||
|
||||
// Enhanced stack trace
|
||||
if (context.enhancedStack) {
|
||||
console.log('Enhanced Stack Trace:\n' + context.enhancedStack);
|
||||
if (isWarning) {
|
||||
// Simplified warning output
|
||||
this.logger.warn(error.message, context);
|
||||
return;
|
||||
}
|
||||
|
||||
// API-specific information
|
||||
if (isApiError && error.context) {
|
||||
console.log('API Context:', error.context);
|
||||
}
|
||||
|
||||
// Environment information
|
||||
console.log('Environment:', {
|
||||
mode: process.env.NODE_ENV,
|
||||
nextPublicMode: process.env.NEXT_PUBLIC_GRIDPILOT_MODE,
|
||||
version: process.env.NEXT_PUBLIC_APP_VERSION,
|
||||
buildTime: process.env.NEXT_PUBLIC_BUILD_TIME,
|
||||
});
|
||||
|
||||
// Performance metrics
|
||||
if (context.memory && typeof context.memory === 'object' &&
|
||||
'usedJSHeapSize' in context.memory && 'totalJSHeapSize' in context.memory) {
|
||||
const memory = context.memory as { usedJSHeapSize: number; totalJSHeapSize: number };
|
||||
console.log('Memory Usage:', {
|
||||
used: `${(memory.usedJSHeapSize / 1024 / 1024).toFixed(2)} MB`,
|
||||
total: `${(memory.totalJSHeapSize / 1024 / 1024).toFixed(2)} MB`,
|
||||
});
|
||||
}
|
||||
|
||||
// Network information
|
||||
if (context.connection) {
|
||||
console.log('Network:', context.connection);
|
||||
}
|
||||
|
||||
// Error history (last 5 errors)
|
||||
if (this.errorHistory.length > 0) {
|
||||
console.log('Recent Error History:', this.errorHistory.slice(-5));
|
||||
}
|
||||
|
||||
console.groupEnd();
|
||||
|
||||
// Also log to our logger
|
||||
// Full error details for actual errors
|
||||
this.logger.error(error.message, error, context);
|
||||
|
||||
// In development, show additional details
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
console.groupCollapsed(`%c[ERROR DETAIL] ${error.name || 'Error'}`, 'color: #ff4444; font-weight: bold; font-size: 14px;');
|
||||
|
||||
console.log('%cError Details:', 'color: #666; font-weight: bold;', {
|
||||
name: error.name,
|
||||
message: error.message,
|
||||
type: isApiError ? error.type : 'N/A',
|
||||
severity: isApiError ? error.getSeverity() : 'error',
|
||||
retryable: isApiError ? error.isRetryable() : 'N/A',
|
||||
connectivity: isApiError ? error.isConnectivityIssue() : 'N/A',
|
||||
});
|
||||
|
||||
console.log('%cContext:', 'color: #666; font-weight: bold;', context);
|
||||
|
||||
if (isApiError && error.context?.developerHint) {
|
||||
console.log('%c💡 Developer Hint:', 'color: #00aaff; font-weight: bold;', error.context.developerHint);
|
||||
}
|
||||
|
||||
console.groupEnd();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -444,7 +433,17 @@ export class GlobalErrorHandler {
|
||||
report(error: Error | ApiError, additionalContext: Record<string, unknown> = {}): void {
|
||||
const context = this.captureEnhancedContext('manual_report', additionalContext);
|
||||
|
||||
this.logErrorWithMaximumDetail(error, context);
|
||||
// Check if this is a network error (don't report to external services)
|
||||
const isNetworkError = error.message.includes('fetch') ||
|
||||
error.message.includes('Failed to fetch') ||
|
||||
error.message.includes('NetworkError');
|
||||
|
||||
if (isNetworkError) {
|
||||
this.logger.warn(`Manual error report: ${error.message}`, context);
|
||||
} else {
|
||||
this.logErrorWithMaximumDetail(error, context);
|
||||
}
|
||||
|
||||
this.addToHistory(error, context);
|
||||
|
||||
// Auto-capture for replay in development
|
||||
@@ -453,7 +452,8 @@ export class GlobalErrorHandler {
|
||||
replaySystem.autoCapture(error, context);
|
||||
}
|
||||
|
||||
if (this.options.reportToExternal) {
|
||||
// Only report non-network errors to external services
|
||||
if (this.options.reportToExternal && !isNetworkError) {
|
||||
this.reportToExternal(error, context);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user