logging.ts 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /**
  2. * Log an error
  3. *
  4. * The {@link message} property describes what went wrong. Generally (but not
  5. * always) in such situations we also have an "error" object that has specific
  6. * details about the issue - that gets passed as the second parameter.
  7. *
  8. * Note that the "error" {@link e} is not typed. This is because in JavaScript
  9. * any arbitrary value can be thrown. So this function allows us to pass it an
  10. * arbitrary value as the error, and will internally figure out how best to deal
  11. * with it.
  12. *
  13. * Where and how this error gets logged is dependent on where this code is
  14. * running. The default implementation logs a string to the console, but in
  15. * practice the layers above us will use the hooks provided in this file to
  16. * route and show this error elsewhere.
  17. *
  18. * TODO (MR): Currently this is a placeholder function to funnel error logs
  19. * through. This needs to do what the existing logError in @ente/shared does,
  20. * but it cannot have a direct Electron/Sentry dependency here. For now, we just
  21. * log on the console.
  22. */
  23. export const logError = (message: string, e?: unknown) => {
  24. if (e === undefined || e === null) {
  25. console.error(message);
  26. return;
  27. }
  28. let es: string;
  29. if (e instanceof Error) {
  30. // In practice, we expect ourselves to be called with Error objects, so
  31. // this is the happy path so to say.
  32. es = `${e.name}: ${e.message}\n${e.stack}`;
  33. } else {
  34. // For the rest rare cases, use the default string serialization of e.
  35. es = String(e);
  36. }
  37. console.error(`${message}: ${es}`);
  38. };