utils.ts 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import ElectronAPIs from '@ente/shared/electron';
  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. export async function getSentryUserID() {
  11. if (isElectron()) {
  12. return await ElectronAPIs.getSentryUserID();
  13. } else {
  14. let anonymizeUserID = getLocalSentryUserID();
  15. if (!anonymizeUserID) {
  16. anonymizeUserID = makeID(6);
  17. setLocalSentryUserID(anonymizeUserID);
  18. }
  19. return anonymizeUserID;
  20. }
  21. }
  22. function makeID(length) {
  23. let result = '';
  24. const characters =
  25. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
  26. const charactersLength = characters.length;
  27. for (let i = 0; i < length; i++) {
  28. result += characters.charAt(
  29. Math.floor(Math.random() * charactersLength)
  30. );
  31. }
  32. return result;
  33. }
  34. export function isErrorUnnecessaryForSentry(error: any) {
  35. if (error?.message?.includes('Network Error')) {
  36. return true;
  37. } else if (error?.status === 401) {
  38. return true;
  39. }
  40. return false;
  41. }
  42. export const getIsSentryEnabled = () => {
  43. const isAppENVDevelopment = getAppEnv() === APP_ENV.DEVELOPMENT;
  44. const isSentryDisabled = isDisableSentryFlagSet();
  45. return !isAppENVDevelopment || !isSentryDisabled;
  46. };