utils.ts 1.6 KB

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