File size: 5,019 Bytes
96af7c9 |
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 |
/**
* Error Helper & Auto-Fix Utility
* ابزار خطایابی و تصحیح خودکار
*/
class ErrorHelper {
constructor() {
this.errorHistory = [];
this.autoFixEnabled = true;
}
/**
* Analyze error and suggest fixes
*/
analyzeError(error, context = {}) {
const analysis = {
error: error.message || String(error),
type: this.detectErrorType(error),
suggestions: [],
autoFix: null,
severity: 'medium'
};
// Common error patterns
if (error.message?.includes('500') || error.message?.includes('Internal Server Error')) {
analysis.suggestions.push('Server error - check backend logs');
analysis.suggestions.push('Try refreshing the page');
analysis.severity = 'high';
}
if (error.message?.includes('404') || error.message?.includes('Not Found')) {
analysis.suggestions.push('Endpoint not found - check API URL');
analysis.suggestions.push('Verify backend is running');
analysis.severity = 'medium';
}
if (error.message?.includes('CORS') || error.message?.includes('cross-origin')) {
analysis.suggestions.push('CORS error - check backend CORS settings');
analysis.severity = 'high';
}
if (error.message?.includes('WebSocket')) {
analysis.suggestions.push('WebSocket connection failed');
analysis.suggestions.push('Check if WebSocket endpoint is available');
analysis.autoFix = () => this.reconnectWebSocket();
analysis.severity = 'medium';
}
if (error.message?.includes('symbol') || error.message?.includes('BTC')) {
analysis.suggestions.push('Invalid symbol - try BTC, ETH, SOL, etc.');
analysis.autoFix = () => this.fixSymbol(context.symbol);
analysis.severity = 'low';
}
this.errorHistory.push({
...analysis,
timestamp: new Date().toISOString(),
context
});
return analysis;
}
detectErrorType(error) {
const msg = String(error.message || error).toLowerCase();
if (msg.includes('network') || msg.includes('fetch')) return 'network';
if (msg.includes('500') || msg.includes('server')) return 'server';
if (msg.includes('404') || msg.includes('not found')) return 'not_found';
if (msg.includes('cors')) return 'cors';
if (msg.includes('websocket')) return 'websocket';
if (msg.includes('timeout')) return 'timeout';
return 'unknown';
}
/**
* Auto-fix common issues
*/
async autoFix(error, context = {}) {
if (!this.autoFixEnabled) return false;
const analysis = this.analyzeError(error, context);
if (analysis.autoFix) {
try {
await analysis.autoFix();
return true;
} catch (e) {
console.error('Auto-fix failed:', e);
return false;
}
}
// Generic fixes
if (analysis.type === 'network') {
// Retry after delay
await new Promise(resolve => setTimeout(resolve, 1000));
return true;
}
return false;
}
fixSymbol(symbol) {
if (!symbol) return 'BTC';
// Remove spaces, convert to uppercase
return symbol.trim().toUpperCase().replace(/\s+/g, '');
}
async reconnectWebSocket() {
// Access wsClient from window or import
if (typeof window !== 'undefined' && window.wsClient) {
window.wsClient.disconnect();
await new Promise(resolve => setTimeout(resolve, 1000));
window.wsClient.connect();
return true;
}
return false;
}
/**
* Get error statistics
*/
getStats() {
const types = {};
this.errorHistory.forEach(err => {
types[err.type] = (types[err.type] || 0) + 1;
});
return {
total: this.errorHistory.length,
byType: types,
recent: this.errorHistory.slice(-10)
};
}
/**
* Clear error history
*/
clear() {
this.errorHistory = [];
}
}
// Global error helper instance
const errorHelper = new ErrorHelper();
// Auto-catch unhandled errors
window.addEventListener('error', (event) => {
errorHelper.analyzeError(event.error || event.message, {
filename: event.filename,
lineno: event.lineno,
colno: event.colno
});
});
window.addEventListener('unhandledrejection', (event) => {
errorHelper.analyzeError(event.reason, {
type: 'unhandled_promise_rejection'
});
});
export default errorHelper;
|