1. function wrapLogEntries(logStream, fallbackFunction) {
  2. if (!logStream || typeof logStream.log !== 'function') {
  3. return logStream; // No wrapping needed
  4. }
  5. const wrappedLogStream = {
  6. log: function (...args) {
  7. try {
  8. // Attempt to log using the original method
  9. logStream.log(...args);
  10. } catch (error) {
  11. // Wrap the error with a custom object
  12. const wrappedError = {
  13. type: 'wrapped',
  14. message: error.message,
  15. stack: error.stack,
  16. originalError: error,
  17. };
  18. // Call the fallback function with the wrapped error
  19. if (typeof fallbackFunction === 'function') {
  20. fallbackFunction(wrappedError);
  21. } else {
  22. console.error("Fallback function not provided. Original error:", error);
  23. }
  24. }
  25. },
  26. };
  27. return wrappedLogStream;
  28. }

Add your comment