diff --git a/src/utils/errorCapture.ts b/src/utils/errorCapture.ts index f0153c96..2df0b8bc 100644 --- a/src/utils/errorCapture.ts +++ b/src/utils/errorCapture.ts @@ -6,7 +6,7 @@ declare const window: { captureMessage: (message: string) => void; }; -export const captureException = (exception: any) => { +export const captureException = (exception: Error) => { if (window.captureException) { window.captureException(exception); } @@ -16,3 +16,11 @@ export const captureMessage = (message: string) => { window.captureMessage(message); } }; + +export const captureMaybeError = (errorMessage: string, error: Error | any) => { + if (error instanceof Error) { + captureException(new Error(errorMessage, error)); + } else { + captureMessage(errorMessage); + } +}; diff --git a/src/view/dialogProvider/dialogStore.ts b/src/view/dialogProvider/dialogStore.ts index 1ab47663..f3b4edee 100644 --- a/src/view/dialogProvider/dialogStore.ts +++ b/src/view/dialogProvider/dialogStore.ts @@ -11,7 +11,7 @@ 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'; +import { captureException, captureMaybeError, captureMessage } from '../../utils/errorCapture'; export const initDialogState: DialogState = { status: Status.INITIAL, @@ -61,7 +61,7 @@ export const useDialogStore = create( return dialoger; }) .catch((e) => { - captureMessage(`Kunne ikke hente dialogdata ${e.toString()}`); + captureMaybeError(`Kunne ikke hente dialogdata ${e.toString()}`, e); set( (prevState) => ({ ...prevState, status: Status.ERROR, error: e }), false, @@ -147,7 +147,7 @@ export const useDialogStore = create( } } catch (e) { if (e instanceof UnautorizedError) { - captureMessage('UnautorizedError 401 på henting av sist oppdatert'); + captureMessage('UnauthorizedError 401 på henting av sist oppdatert'); } else { captureMessage('Kunne ikke hente sist oppdatert'); } @@ -207,7 +207,7 @@ export const useDialogStore = create( return dialog; }) .catch((err) => { - console.error('Kunne ikke sende melding', err); + captureMaybeError(`Kunne ikke sende melding: ${err.toString()}`, err); set({ status: Status.ERROR }, false, 'sendMelding/rejected'); return undefined; });