Create src/utils/errorLogger.js
Browse files- src/utils/errorLogger.js +126 -0
src/utils/errorLogger.js
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Task 8.1: Standardized Error Logging Utility
|
| 3 |
+
* Provides consistent error logging across all components
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
export class ErrorLogger {
|
| 7 |
+
constructor(componentName) {
|
| 8 |
+
this.componentName = componentName;
|
| 9 |
+
}
|
| 10 |
+
|
| 11 |
+
/**
|
| 12 |
+
* Log an error with standardized format
|
| 13 |
+
* @param {string} message - Error message
|
| 14 |
+
* @param {Object} details - Additional error details
|
| 15 |
+
* @param {Error} error - Original error object (optional)
|
| 16 |
+
*/
|
| 17 |
+
log(message, details = {}, error = null) {
|
| 18 |
+
const logEntry = {
|
| 19 |
+
timestamp: new Date().toISOString(),
|
| 20 |
+
component: this.componentName,
|
| 21 |
+
level: 'error',
|
| 22 |
+
message: message,
|
| 23 |
+
details: details,
|
| 24 |
+
stack: error?.stack || new Error().stack
|
| 25 |
+
};
|
| 26 |
+
|
| 27 |
+
// Log to console with formatting
|
| 28 |
+
console.error(`[${logEntry.timestamp}] [${logEntry.component}] ERROR:`, message);
|
| 29 |
+
|
| 30 |
+
if (Object.keys(details).length > 0) {
|
| 31 |
+
console.error('Details:', details);
|
| 32 |
+
}
|
| 33 |
+
|
| 34 |
+
if (error) {
|
| 35 |
+
console.error('Original error:', error.message);
|
| 36 |
+
console.error('Stack trace:', error.stack);
|
| 37 |
+
}
|
| 38 |
+
|
| 39 |
+
return logEntry;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/**
|
| 43 |
+
* Log a validation error with expected vs actual schema
|
| 44 |
+
* @param {string} field - Field that failed validation
|
| 45 |
+
* @param {*} expected - Expected value/type
|
| 46 |
+
* @param {*} actual - Actual value/type
|
| 47 |
+
*/
|
| 48 |
+
logValidationError(field, expected, actual) {
|
| 49 |
+
return this.log('Validation Error', {
|
| 50 |
+
field: field,
|
| 51 |
+
expected: expected,
|
| 52 |
+
actual: actual,
|
| 53 |
+
type: 'validation'
|
| 54 |
+
});
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
/**
|
| 58 |
+
* Log a warning (non-fatal issue)
|
| 59 |
+
* @param {string} message - Warning message
|
| 60 |
+
* @param {Object} details - Additional details
|
| 61 |
+
*/
|
| 62 |
+
warn(message, details = {}) {
|
| 63 |
+
const logEntry = {
|
| 64 |
+
timestamp: new Date().toISOString(),
|
| 65 |
+
component: this.componentName,
|
| 66 |
+
level: 'warning',
|
| 67 |
+
message: message,
|
| 68 |
+
details: details
|
| 69 |
+
};
|
| 70 |
+
|
| 71 |
+
console.warn(`[${logEntry.timestamp}] [${logEntry.component}] WARNING:`, message);
|
| 72 |
+
|
| 73 |
+
if (Object.keys(details).length > 0) {
|
| 74 |
+
console.warn('Details:', details);
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
return logEntry;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/**
|
| 81 |
+
* Log an info message
|
| 82 |
+
* @param {string} message - Info message
|
| 83 |
+
* @param {Object} details - Additional details
|
| 84 |
+
*/
|
| 85 |
+
info(message, details = {}) {
|
| 86 |
+
const logEntry = {
|
| 87 |
+
timestamp: new Date().toISOString(),
|
| 88 |
+
component: this.componentName,
|
| 89 |
+
level: 'info',
|
| 90 |
+
message: message,
|
| 91 |
+
details: details
|
| 92 |
+
};
|
| 93 |
+
|
| 94 |
+
console.log(`[${logEntry.timestamp}] [${logEntry.component}] INFO:`, message);
|
| 95 |
+
|
| 96 |
+
if (Object.keys(details).length > 0) {
|
| 97 |
+
console.log('Details:', details);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
return logEntry;
|
| 101 |
+
}
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
/**
|
| 105 |
+
* Create a logger instance for a component
|
| 106 |
+
* @param {string} componentName - Name of the component
|
| 107 |
+
* @returns {ErrorLogger} Logger instance
|
| 108 |
+
*/
|
| 109 |
+
export function createLogger(componentName) {
|
| 110 |
+
return new ErrorLogger(componentName);
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
/**
|
| 114 |
+
* Format validation errors with expected vs actual schema
|
| 115 |
+
* @param {Array} errors - Array of validation errors
|
| 116 |
+
* @returns {string} Formatted error message
|
| 117 |
+
*/
|
| 118 |
+
export function formatValidationErrors(errors) {
|
| 119 |
+
if (!errors || errors.length === 0) {
|
| 120 |
+
return 'No validation errors';
|
| 121 |
+
}
|
| 122 |
+
|
| 123 |
+
return errors.map((err, index) => {
|
| 124 |
+
return `${index + 1}. ${err.field}: Expected ${err.expected}, got ${err.actual}`;
|
| 125 |
+
}).join('\n');
|
| 126 |
+
}
|