-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-monitor.js
More file actions
183 lines (154 loc) · 5.31 KB
/
Copy patherror-monitor.js
File metadata and controls
183 lines (154 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* Simple Error Monitoring System
* Tracks and logs errors for debugging and monitoring
*/
class ErrorMonitor {
constructor() {
this.errors = [];
this.maxErrors = 100; // Keep last 100 errors
this.isProduction = window.location.hostname !== 'localhost' && !window.location.hostname.includes('127.0.0.1');
this.init();
}
init() {
// Capture unhandled errors
window.addEventListener('error', (event) => {
this.logError({
type: 'unhandled_error',
message: event.message,
filename: event.filename,
lineno: event.lineno,
colno: event.colno,
error: event.error?.stack || event.error
});
});
// Capture unhandled promise rejections
window.addEventListener('unhandledrejection', (event) => {
this.logError({
type: 'unhandled_rejection',
message: event.reason?.message || event.reason,
stack: event.reason?.stack
});
});
// Capture console.error calls
this.wrapConsoleError();
}
logError(errorData) {
const errorEntry = {
timestamp: new Date().toISOString(),
url: window.location.href,
userAgent: navigator.userAgent,
...errorData
};
this.errors.push(errorEntry);
// Keep only last N errors
if (this.errors.length > this.maxErrors) {
this.errors.shift();
}
// Log to console in development
if (!this.isProduction) {
console.error('🚨 Error logged:', errorEntry);
}
// Store in localStorage for debugging
try {
localStorage.setItem('errorLog', JSON.stringify(this.errors));
} catch (e) {
// Ignore localStorage errors
}
// In production, could send to external service
if (this.isProduction) {
this.sendToMonitoring(errorEntry);
}
}
wrapConsoleError() {
const originalError = console.error;
console.error = (...args) => {
// Call original
originalError.apply(console, args);
// Log to our system
this.logError({
type: 'console_error',
message: args.map(arg =>
typeof arg === 'object' ? JSON.stringify(arg) : String(arg)
).join(' ')
});
};
}
sendToMonitoring(errorEntry) {
// Could send to Sentry, LogRocket, or custom endpoint
// For now, just use GitHub Issues API (optional)
// Example: Send critical errors to GitHub Issues
if (this.isCriticalError(errorEntry)) {
console.warn('Critical error detected:', errorEntry.message);
// Could POST to /api/report-error endpoint
}
}
isCriticalError(errorEntry) {
const criticalPatterns = [
'failed to fetch',
'network error',
'cannot read property',
'undefined is not',
'null is not',
'script error'
];
const message = errorEntry.message?.toLowerCase() || '';
return criticalPatterns.some(pattern => message.includes(pattern));
}
getErrors() {
return this.errors;
}
getRecentErrors(count = 10) {
return this.errors.slice(-count);
}
clearErrors() {
this.errors = [];
try {
localStorage.removeItem('errorLog');
} catch (e) {
// Ignore
}
}
exportErrors() {
const data = JSON.stringify(this.errors, null, 2);
const blob = new Blob([data], { type: 'application/json' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `error-log-${Date.now()}.json`;
a.click();
URL.revokeObjectURL(url);
}
getStats() {
const stats = {
total: this.errors.length,
byType: {},
recent: this.getRecentErrors(5),
criticalCount: 0
};
this.errors.forEach(error => {
// Count by type
stats.byType[error.type] = (stats.byType[error.type] || 0) + 1;
// Count critical
if (this.isCriticalError(error)) {
stats.criticalCount++;
}
});
return stats;
}
}
// Initialize error monitor
const errorMonitor = new ErrorMonitor();
// Expose to window for debugging
window.errorMonitor = errorMonitor;
// Add helper commands for console
console.log('🔍 Error Monitoring Active!');
console.log('Commands:');
console.log(' errorMonitor.getErrors() - View all errors');
console.log(' errorMonitor.getRecentErrors(10) - View recent errors');
console.log(' errorMonitor.getStats() - View error statistics');
console.log(' errorMonitor.clearErrors() - Clear error log');
console.log(' errorMonitor.exportErrors() - Export errors to file');
// Export for use in other scripts
if (typeof module !== 'undefined' && module.exports) {
module.exports = ErrorMonitor;
}