The error to format (can be any type)
Optionalprefix: stringOptional prefix for context (e.g., 'API', 'Database', 'Widget')
A formatted error message string
// Without prefix
formatError(new Error('Connection failed'))
// 'Connection failed'
// With prefix for context
formatError(new Error('Timeout'), 'API Call')
// 'API Call: Timeout'
formatError('Invalid input', 'Validation')
// 'Validation: Invalid input'
// Usage in service methods
try {
await fetchData();
} catch (err) {
const message = formatError(err, 'DataService');
console.error(message); // 'DataService: Network error'
}
Formats an error for consistent logging with optional prefix
Converts errors to formatted strings suitable for logging, with optional contextual prefix for better error traceability. Handles unknown error types safely.