index.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { ApiError, errorWithContext } from "@ente/shared/error";
  2. import { addLocalLog, addLogLine } from "@ente/shared/logging";
  3. import {
  4. getSentryUserID,
  5. isErrorUnnecessaryForSentry,
  6. } from "@ente/shared/sentry/utils";
  7. import InMemoryStore, { MS_KEYS } from "@ente/shared/storage/InMemoryStore";
  8. import { getHasOptedOutOfCrashReports } from "@ente/shared/storage/localStorage/helpers";
  9. import * as Sentry from "@sentry/nextjs";
  10. export const logError = async (
  11. error: any,
  12. msg: string,
  13. info?: Record<string, unknown>,
  14. skipAddLogLine = false,
  15. ) => {
  16. const err = errorWithContext(error, msg);
  17. if (!skipAddLogLine) {
  18. if (error instanceof ApiError) {
  19. addLogLine(`error: ${error?.name} ${error?.message}
  20. msg: ${msg} errorCode: ${JSON.stringify(error?.errCode)}
  21. httpStatusCode: ${JSON.stringify(error?.httpStatusCode)} ${
  22. info ? `info: ${JSON.stringify(info)}` : ""
  23. }
  24. ${error?.stack}`);
  25. } else {
  26. addLogLine(
  27. `error: ${error?.name} ${error?.message}
  28. msg: ${msg} ${info ? `info: ${JSON.stringify(info)}` : ""}
  29. ${error?.stack}`,
  30. );
  31. }
  32. }
  33. if (!InMemoryStore.has(MS_KEYS.OPT_OUT_OF_CRASH_REPORTS)) {
  34. const optedOutOfCrashReports = getHasOptedOutOfCrashReports();
  35. InMemoryStore.set(
  36. MS_KEYS.OPT_OUT_OF_CRASH_REPORTS,
  37. optedOutOfCrashReports,
  38. );
  39. }
  40. if (InMemoryStore.get(MS_KEYS.OPT_OUT_OF_CRASH_REPORTS)) {
  41. addLocalLog(() => `skipping sentry error: ${error?.name}`);
  42. return;
  43. }
  44. if (isErrorUnnecessaryForSentry(error)) {
  45. return;
  46. }
  47. Sentry.captureException(err, {
  48. level: "info",
  49. user: { id: await getSentryUserID() },
  50. contexts: {
  51. ...(info && {
  52. info: info,
  53. }),
  54. rootCause: { message: error?.message, completeError: error },
  55. },
  56. });
  57. };