Skip to content

Commit

Permalink
Refactor error message capture
Browse files Browse the repository at this point in the history
  • Loading branch information
tu55eladd committed Dec 20, 2024
1 parent 4d9f461 commit 642e5f2
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/felleskomponenter/logging.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DialogApi } from '../api/UseApiBasePath';
import { logAmplitudeEvent } from '../metrics/amplitude-utils';
import { DialogData } from '../utils/Typer';
import { captureMessage } from '@sentry/react';

interface FrontendEvent {
name: string;
Expand All @@ -20,8 +21,8 @@ export default function loggEvent(eventNavn: string, feltObjekt?: object, tagObj
body: JSON.stringify(event)
};
logAmplitudeEvent(eventNavn, { ...feltObjekt, ...tagObjekt });
fetch(url, config).catch((e) => {
console.warn('Klarte ikke logge event', e);
fetch(url, config).catch((e: Error) => {
captureMessage(`Klarte ikke logge event ${e.toString()}`);
});
}

Expand Down
18 changes: 18 additions & 0 deletions src/utils/errorCapture.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* Capture error without importing sentry */

/* Make sure to not import sentry for types, just create a similar type */
declare const window: {
captureException: (exception: any) => void;
captureMessage: (message: string) => void;
};

export const captureException = (exception: any) => {
if (window.captureException) {
window.captureException(exception);
}
};
export const captureMessage = (message: string) => {
if (window.captureMessage) {
window.captureMessage(message);
}
};
15 changes: 3 additions & 12 deletions src/view/dialogProvider/dialogStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,14 @@ import { useShallow } from 'zustand/react/shallow';
import { hentDialogerGraphql } from './dialogGraphql';
import { eqKladd, KladdStore } from '../KladdProvider';
import { UnautorizedError } from '../../utils/fetchErrors';
import { captureMessage } from '../../utils/errorCapture';

export const initDialogState: DialogState = {
status: Status.INITIAL,
sistOppdatert: new Date(),
dialoger: []
};

/* Make sure to not import sentry, just create a similar type */
declare const window: {
captureException: (exception: any) => void;
captureMessage: (message: string) => void;
};

type DialogStore = DialogState &
KladdStore & {
kladder: KladdData[];
Expand Down Expand Up @@ -153,9 +148,7 @@ export const useDialogStore = create(
} catch (e) {
if (e instanceof UnautorizedError) {
} else {
if (window.captureMessage) {
window.captureMessage('Kunne ikke hente sist oppdatert');
}
captureMessage('Kunne ikke hente sist oppdatert');
}
}
},
Expand Down Expand Up @@ -271,9 +264,7 @@ const onIntervalWithCleanup = (pollForChanges: () => Promise<void>) => {
let interval: NodeJS.Timeout;
interval = setInterval(() => {
pollForChanges().catch((e) => {
if (window.captureMessage) {
window.captureMessage('Klarte ikke polle sistOppdatert');
}
captureMessage('Klarte ikke polle sistOppdatert');
clearInterval(interval);
});
}, 10000);
Expand Down

0 comments on commit 642e5f2

Please sign in to comment.