utils.ts 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { WorkerSafeElectronService } from '@ente/shared/electron/service';
  2. import {
  3. getLocalSentryUserID,
  4. setLocalSentryUserID,
  5. } from '@ente/shared/storage/localStorage/helpers';
  6. import isElectron from 'is-electron';
  7. import { getAppEnv } from '@ente/shared/apps/env';
  8. import { APP_ENV } from '@ente/shared/apps/constants';
  9. import { isDisableSentryFlagSet } from '@ente/shared/apps/env';
  10. import { ApiError } from '../error';
  11. import { HttpStatusCode } from 'axios';
  12. export async function getSentryUserID() {
  13. if (isElectron()) {
  14. return await WorkerSafeElectronService.getSentryUserID();
  15. } else {
  16. let anonymizeUserID = getLocalSentryUserID();
  17. if (!anonymizeUserID) {
  18. anonymizeUserID = makeID(6);
  19. setLocalSentryUserID(anonymizeUserID);
  20. }
  21. return anonymizeUserID;
  22. }
  23. }
  24. function makeID(length) {
  25. let result = '';
  26. const characters =
  27. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  28. const charactersLength = characters.length;
  29. for (let i = 0; i < length; i++) {
  30. result += characters.charAt(
  31. Math.floor(Math.random() * charactersLength)
  32. );
  33. }
  34. return result;
  35. }
  36. export function isErrorUnnecessaryForSentry(error: any) {
  37. if (error?.message?.includes('Network Error')) {
  38. return true;
  39. } else if (
  40. error instanceof ApiError &&
  41. error.httpStatusCode === HttpStatusCode.Unauthorized
  42. ) {
  43. return true;
  44. }
  45. return false;
  46. }
  47. export const getIsSentryEnabled = () => {
  48. const isAppENVDevelopment = getAppEnv() === APP_ENV.DEVELOPMENT;
  49. const isSentryDisabled = isDisableSentryFlagSet();
  50. return !isAppENVDevelopment && !isSentryDisabled;
  51. };