File size: 10,818 Bytes
b2c626c | 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 | /**
* error_handler - Centralized error handling
* Removes repetitive error handling patterns across the project
*/
import { createModuleLogger } from "../log_system/log_funcs.js";
const log = createModuleLogger('error_handler');
/**
* Application error types
*/
export const ErrorTypes = {
VALIDATION: 'VALIDATION_ERROR',
NETWORK: 'NETWORK_ERROR',
FILE_IO: 'FILE_IO_ERROR',
CANVAS: 'CANVAS_ERROR',
IMAGE_PROCESSING: 'IMAGE_PROCESSING_ERROR',
STATE_MANAGEMENT: 'STATE_MANAGEMENT_ERROR',
USER_INPUT: 'USER_INPUT_ERROR',
SYSTEM: 'SYSTEM_ERROR'
};
/**
* Application error class with additional information
*/
export class AppError extends Error {
constructor(message, type = ErrorTypes.SYSTEM, details = null, originalError = null) {
super(message);
this.name = 'AppError';
this.type = type;
this.details = details;
this.originalError = originalError;
this.timestamp = new Date().toISOString();
if (Error.captureStackTrace) {
Error.captureStackTrace(this, AppError);
}
}
}
/**
* Error handler with automatic logging and categorization
*/
export class ErrorHandler {
constructor() {
this.errorCounts = new Map();
this.errorHistory = [];
this.maxHistorySize = 100;
}
/**
* Handles an error with automatic logging
* @param {Error | AppError | string} error - Error to handle
* @param {string} context - Error occurrence context
* @param {object} additionalInfo - Additional information
* @returns {AppError} Normalized error
*/
handle(error, context = 'Unknown', additionalInfo = {}) {
const normalizedError = this.normalizeError(error, context, additionalInfo);
this.logError(normalizedError, context);
this.recordError(normalizedError);
this.incrementErrorCount(normalizedError.type);
return normalizedError;
}
/**
* Normalizes an error to the standard format
* @param {Error | AppError | string} error - Error to normalize
* @param {string} context - Context
* @param {object} additionalInfo - Additional information
* @returns {AppError} Normalized error
*/
normalizeError(error, context, additionalInfo) {
if (error instanceof AppError) {
return error;
}
if (error instanceof Error) {
const type = this.categorizeError(error, context);
return new AppError(error.message, type, { context, ...additionalInfo }, error);
}
if (typeof error === 'string') {
return new AppError(error, ErrorTypes.SYSTEM, { context, ...additionalInfo });
}
return new AppError('Unknown error occurred', ErrorTypes.SYSTEM, { context, originalError: error, ...additionalInfo });
}
/**
* Categorizes an error based on the message and context
* @param {Error} error - Error to categorize
* @param {string} context - Context
* @returns {ErrorType} Error type
*/
categorizeError(error, context) {
const message = error.message.toLowerCase();
if (message.includes('fetch') || message.includes('network') ||
message.includes('connection') || message.includes('timeout')) {
return ErrorTypes.NETWORK;
}
if (message.includes('file') || message.includes('read') ||
message.includes('write') || message.includes('path')) {
return ErrorTypes.FILE_IO;
}
if (message.includes('invalid') || message.includes('required') ||
message.includes('validation') || message.includes('format')) {
return ErrorTypes.VALIDATION;
}
if (message.includes('image') || message.includes('canvas') ||
message.includes('blob') || message.includes('tensor')) {
return ErrorTypes.IMAGE_PROCESSING;
}
if (message.includes('state') || message.includes('cache') ||
message.includes('storage')) {
return ErrorTypes.STATE_MANAGEMENT;
}
if (context.toLowerCase().includes('canvas')) {
return ErrorTypes.CANVAS;
}
return ErrorTypes.SYSTEM;
}
/**
* Logs an error with the appropriate level
* @param {AppError} error - Error to log
* @param {string} context - Context
*/
logError(error, context) {
const logMessage = `[${error.type}] ${error.message}`;
const logDetails = {
context,
timestamp: error.timestamp,
details: error.details,
stack: error.stack
};
switch (error.type) {
case ErrorTypes.VALIDATION:
case ErrorTypes.USER_INPUT:
log.warn(logMessage, logDetails);
break;
case ErrorTypes.NETWORK:
log.error(logMessage, logDetails);
break;
default:
log.error(logMessage, logDetails);
}
}
/**
* Records an error in history
* @param {AppError} error - Error to record
*/
recordError(error) {
this.errorHistory.push({
timestamp: error.timestamp,
type: error.type,
message: error.message,
context: error.details?.context
});
if (this.errorHistory.length > this.maxHistorySize) {
this.errorHistory.shift();
}
}
/**
* Increments the error count for the given type
* @param {ErrorType} errorType - Error type
*/
incrementErrorCount(errorType) {
const current = this.errorCounts.get(errorType) || 0;
this.errorCounts.set(errorType, current + 1);
}
/**
* Returns error statistics
* @returns {ErrorStats} Error statistics
*/
getErrorStats() {
const errorCountsObj = {};
for (const [key, value] of this.errorCounts.entries()) {
errorCountsObj[key] = value;
}
return {
totalErrors: this.errorHistory.length,
errorCounts: errorCountsObj,
recentErrors: this.errorHistory.slice(-10),
errorsByType: this.groupErrorsByType()
};
}
/**
* Groups errors by type
* @returns {{ [key: string]: ErrorHistoryEntry[] }} Errors grouped by type
*/
groupErrorsByType() {
const grouped = {};
this.errorHistory.forEach((error) => {
if (!grouped[error.type]) {
grouped[error.type] = [];
}
grouped[error.type].push(error);
});
return grouped;
}
/**
* Clears the error history
*/
clearHistory() {
this.errorHistory = [];
this.errorCounts.clear();
log.info('Error history cleared');
}
}
const errorHandler = new ErrorHandler();
/**
* Function wrapper with automatic error handling
* @param {Function} fn - Function to wrap
* @param {string} context - Execution context
* @returns {Function} Wrapped function
*/
export function withErrorHandling(fn, context) {
return async function (...args) {
try {
return await fn.apply(this, args);
}
catch (error) {
const handledError = errorHandler.handle(error, context, {
functionName: fn.name,
arguments: args.length
});
throw handledError;
}
};
}
/**
* Decorator for class methods with automatic error handling
* @param {string} context - Execution context
*/
export function handleErrors(context) {
return function (target, propertyKey, descriptor) {
const originalMethod = descriptor.value;
descriptor.value = async function (...args) {
try {
return await originalMethod.apply(this, args);
}
catch (error) {
const handledError = errorHandler.handle(error, `${context}.${propertyKey}`, {
className: target.constructor.name,
methodName: propertyKey,
arguments: args.length
});
throw handledError;
}
};
return descriptor;
};
}
/**
* Helper function for creating validation errors
* @param {string} message - Error message
* @param {object} details - Validation details
* @returns {AppError} Validation error
*/
export function createValidationError(message, details = {}) {
return new AppError(message, ErrorTypes.VALIDATION, details);
}
/**
* Helper function for creating network errors
* @param {string} message - Error message
* @param {object} details - Network details
* @returns {AppError} Network error
*/
export function createNetworkError(message, details = {}) {
return new AppError(message, ErrorTypes.NETWORK, details);
}
/**
* Helper function for creating file errors
* @param {string} message - Error message
* @param {object} details - File details
* @returns {AppError} File error
*/
export function createFileError(message, details = {}) {
return new AppError(message, ErrorTypes.FILE_IO, details);
}
/**
* Helper function for safely executing an operation
* @param {() => Promise<T>} operation - Operation to execute
* @param {T} fallbackValue - Fallback value in case of error
* @param {string} context - Operation context
* @returns {Promise<T>} Operation result or fallback value
*/
export async function safeExecute(operation, fallbackValue, context = 'SafeExecute') {
try {
return await operation();
}
catch (error) {
errorHandler.handle(error, context);
return fallbackValue;
}
}
/**
* Retries an operation with exponential backoff
* @param {() => Promise<T>} operation - Operation to retry
* @param {number} maxRetries - Maximum number of attempts
* @param {number} baseDelay - Base delay in ms
* @param {string} context - Operation context
* @returns {Promise<T>} Operation result
*/
export async function retryWithBackoff(operation, maxRetries = 3, baseDelay = 1000, context = 'RetryOperation') {
let lastError;
for (let attempt = 0; attempt <= maxRetries; attempt++) {
try {
return await operation();
}
catch (error) {
lastError = error;
if (attempt === maxRetries) {
break;
}
const delay = baseDelay * Math.pow(2, attempt);
log.warn(`Attempt ${attempt + 1} failed, retrying in ${delay}ms`, { error: lastError.message, context });
await new Promise(resolve => setTimeout(resolve, delay));
}
}
throw errorHandler.handle(lastError, context, { attempts: maxRetries + 1 });
}
export { errorHandler };
export default errorHandler;
|