-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror-handler.js
More file actions
315 lines (274 loc) · 8.57 KB
/
Copy patherror-handler.js
File metadata and controls
315 lines (274 loc) · 8.57 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/**
* error-handler.js
* Centralized error handling, logging, and user notifications
*
* Manages error toasts, persistent errors, database operation logging, and retry logic.
*/
import { setInLocalStorage, getFromLocalStorage } from './storage-manager.js';
/**
* Show temporary error/success/info toast notification
* @param {string} message - Toast message
* @param {string} type - Toast type: 'error', 'success', 'warning', 'info'
* @param {number} duration - Display duration in milliseconds
* @param {Function} retryCallback - Optional retry callback function
*/
export function showErrorToast(message, type = 'error', duration = 3000, retryCallback = null) {
if (!message) return;
try {
// Create toast container if it doesn't exist
let toastContainer = document.getElementById('toast-container');
if (!toastContainer) {
toastContainer = document.createElement('div');
toastContainer.id = 'toast-container';
document.body.appendChild(toastContainer);
}
// Create toast element
const toast = document.createElement('div');
toast.className = `error-toast ${type}`;
// Build toast content
let content = `<span>${message}</span>`;
if (retryCallback) {
content += '<button class="toast-retry">Retry</button>';
}
content += '<button class="toast-close">×</button>';
toast.innerHTML = content;
// Add to page
toastContainer.appendChild(toast);
// Setup close button
const closeBtn = toast.querySelector('.toast-close');
closeBtn.onclick = () => {
toast.style.animation = 'slideOutTop 0.3s ease-out';
setTimeout(() => toast.remove(), 300);
};
// Setup retry button if provided
if (retryCallback) {
const retryBtn = toast.querySelector('.toast-retry');
retryBtn.onclick = async () => {
try {
await retryCallback();
toast.style.animation = 'slideOutTop 0.3s ease-out';
setTimeout(() => toast.remove(), 300);
} catch (error) {
console.error('Retry failed:', error);
}
};
}
// Auto-close after duration
if (duration > 0) {
const timeout = setTimeout(() => {
if (toast.parentElement) {
toast.style.animation = 'slideOutTop 0.3s ease-out';
setTimeout(() => toast.remove(), 300);
}
}, duration);
// Clear timeout on manual close
closeBtn.onclick = () => {
clearTimeout(timeout);
toast.style.animation = 'slideOutTop 0.3s ease-out';
setTimeout(() => toast.remove(), 300);
};
}
console.log(`[${type.toUpperCase()}]`, message);
} catch (error) {
console.error('Failed to show error toast:', error);
}
}
/**
* Show persistent error modal that requires user interaction
* @param {string} title - Error title
* @param {string} message - Error message
* @param {Function} retryCallback - Optional retry callback
*/
export function showPersistentError(title, message, retryCallback = null) {
try {
// Check if error modal already exists
let errorModal = document.getElementById('errorModal');
if (!errorModal) {
console.warn('Error modal not found in DOM');
// Fallback to alert
alert(`${title}\n\n${message}`);
return;
}
// Set error content
const errorTitle = errorModal.querySelector('h2');
const errorDetails = errorModal.querySelector('#errorDetails');
if (errorTitle) {
errorTitle.textContent = title;
}
if (errorDetails) {
errorDetails.textContent = message;
}
// Show modal
errorModal.style.display = 'flex';
// Setup retry button if callback provided
if (retryCallback) {
const retryButton = errorModal.querySelector('button');
if (retryButton) {
retryButton.onclick = async () => {
try {
await retryCallback();
errorModal.style.display = 'none';
} catch (error) {
console.error('Retry failed:', error);
showErrorToast('Retry failed: ' + error.message, 'error');
}
};
}
}
console.error(`[PERSISTENT ERROR] ${title}: ${message}`);
} catch (error) {
console.error('Failed to show persistent error:', error);
}
}
/**
* Log database operation for monitoring
* @param {string} operation - Operation name
* @param {boolean} success - Success status
* @param {*} data - Operation data for logging
*/
export function logDatabaseOperation(operation, success, data = null) {
const logEntry = {
timestamp: new Date().toISOString(),
operation,
success,
data
};
try {
// Log to console
if (success) {
console.log(`[DB_OP SUCCESS] ${operation}`, data);
} else {
console.error(`[DB_OP FAILED] ${operation}`, data);
}
// Store in localStorage for debugging
const maxLogs = 50;
const logs = getFromLocalStorage('spelldle_db_logs', []);
logs.push(logEntry);
// Keep only recent logs
if (logs.length > maxLogs) {
logs.splice(0, logs.length - maxLogs);
}
setInLocalStorage('spelldle_db_logs', logs);
} catch (error) {
console.error('Failed to log database operation:', error);
}
}
/**
* Log general error
* @param {string} message - Error message
* @param {Error} error - Error object
*/
export function logError(message, error) {
console.error(message, error);
try {
const errorLog = {
timestamp: new Date().toISOString(),
message,
stack: error?.stack || '',
name: error?.name || 'UnknownError'
};
// Store in localStorage
const maxErrors = 20;
const errors = getFromLocalStorage('spelldle_errors', []);
errors.push(errorLog);
if (errors.length > maxErrors) {
errors.splice(0, errors.length - maxErrors);
}
setInLocalStorage('spelldle_errors', errors);
} catch (storageError) {
console.error('Failed to log error to storage:', storageError);
}
}
/**
* Handle storage errors with user feedback
* @param {Error} error - Storage error
* @param {string} context - Context where error occurred
* @param {Function} retryCallback - Optional retry callback
*/
export function handleStorageError(error, context, retryCallback = null) {
console.error(`[STORAGE ERROR] ${context}:`, error);
const message = `Storage error in ${context}: ${error.message}`;
if (error.name === 'QuotaExceededError') {
showErrorToast(
'Storage quota exceeded. Please clear some data.',
'error',
5000
);
} else if (retryCallback) {
showErrorToast(
message,
'warning',
5000,
retryCallback
);
} else {
showErrorToast(message, 'error', 5000);
}
logError(context, error);
}
/**
* Retry an async operation with exponential backoff
* @param {Function} operation - Async function to retry
* @param {number} maxAttempts - Maximum number of attempts
* @param {number} delayMs - Initial delay in milliseconds
* @returns {*} Result of operation
*/
export async function retryOperation(operation, maxAttempts = 3, delayMs = 1000) {
let lastError;
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
try {
console.log(`[RETRY] Attempt ${attempt}/${maxAttempts}`);
return await operation();
} catch (error) {
lastError = error;
console.error(`[RETRY] Attempt ${attempt} failed:`, error.message);
if (attempt === maxAttempts) {
// Last attempt failed
break;
}
// Exponential backoff: 1s, 2s, 4s, etc.
const delay = delayMs * Math.pow(2, attempt - 1);
console.log(`[RETRY] Waiting ${delay}ms before next attempt...`);
await new Promise(resolve => setTimeout(resolve, delay));
}
}
// All attempts failed
throw new Error(`Operation failed after ${maxAttempts} attempts: ${lastError.message}`);
}
/**
* Get all logged errors for debugging
* @returns {Array} Array of logged errors
*/
export function getErrorLogs() {
return getFromLocalStorage('spelldle_errors', []);
}
/**
* Get all logged database operations for debugging
* @returns {Array} Array of logged operations
*/
export function getDatabaseLogs() {
return getFromLocalStorage('spelldle_db_logs', []);
}
/**
* Clear all error logs
*/
export function clearErrorLogs() {
try {
localStorage.removeItem('spelldle_errors');
localStorage.removeItem('spelldle_db_logs');
console.log('Error logs cleared');
} catch (error) {
console.error('Failed to clear error logs:', error);
}
}
export default {
showErrorToast,
showPersistentError,
logDatabaseOperation,
logError,
handleStorageError,
retryOperation,
getErrorLogs,
getDatabaseLogs,
clearErrorLogs
};