function wrapLogEntries(logStream, fallbackFunction) {
if (!logStream || typeof logStream.log !== 'function') {
return logStream; // No wrapping needed
}
const wrappedLogStream = {
log: function (...args) {
try {
// Attempt to log using the original method
logStream.log(...args);
} catch (error) {
// Wrap the error with a custom object
const wrappedError = {
type: 'wrapped',
message: error.message,
stack: error.stack,
originalError: error,
};
// Call the fallback function with the wrapped error
if (typeof fallbackFunction === 'function') {
fallbackFunction(wrappedError);
} else {
console.error("Fallback function not provided. Original error:", error);
}
}
},
};
return wrappedLogStream;
}
Add your comment