diff --git a/apps/kyb-app/src/common/providers/DependenciesProvider/helpers.ts b/apps/kyb-app/src/common/providers/DependenciesProvider/helpers.ts index a067edb35..c7daaa237 100644 --- a/apps/kyb-app/src/common/providers/DependenciesProvider/helpers.ts +++ b/apps/kyb-app/src/common/providers/DependenciesProvider/helpers.ts @@ -1,3 +1,4 @@ +import { isExceptionWillBeHandled } from '@/common/utils/helpers'; import { HTTPError } from 'ky'; interface IErrorBody { @@ -22,6 +23,5 @@ export const getJsonErrors = async (errors: HTTPError[]) => { export const isShouldIgnoreErrors = async (errors: HTTPError[]) => { const errorResponses = await getJsonErrors(errors); - // TODO: We should have different error status for this to avoid strict comparison to error message - return errorResponses.every(({ message }) => message === 'No EndUser is set for this token'); + return errorResponses.every(error => isExceptionWillBeHandled(error as unknown as HTTPError)); }; diff --git a/apps/kyb-app/src/common/utils/helpers.ts b/apps/kyb-app/src/common/utils/helpers.ts new file mode 100644 index 000000000..126557635 --- /dev/null +++ b/apps/kyb-app/src/common/utils/helpers.ts @@ -0,0 +1,5 @@ +import { HTTPError } from 'ky'; + +export const isExceptionWillBeHandled = (error: HTTPError) => { + return error.message === 'No EndUser is set for this token'; +}; diff --git a/apps/kyb-app/src/common/utils/request.ts b/apps/kyb-app/src/common/utils/request.ts index d487a1483..4d2355ada 100644 --- a/apps/kyb-app/src/common/utils/request.ts +++ b/apps/kyb-app/src/common/utils/request.ts @@ -1,6 +1,7 @@ import { getAccessToken } from '@/helpers/get-access-token.helper'; import * as Sentry from '@sentry/react'; import ky, { HTTPError } from 'ky'; +import { isExceptionWillBeHandled } from './helpers'; export const request = ky.create({ prefixUrl: import.meta.env.VITE_API_URL || `${window.location.origin}/api/v1/`, @@ -25,33 +26,40 @@ export const request = ky.create({ try { responseBody = await error.response.clone().text(); - } catch (_) { - /* empty */ - } + const responseJson = await error.response.clone().json(); - Sentry.withScope(function (scope) { - // group errors together based on their request and response - scope.setFingerprint([ - request.method, - request.url, - String(error.response.status), - getAccessToken() || 'anonymous', - ]); - Sentry.setUser({ - id: getAccessToken() || 'anonymous', - }); + const isShouldIgnore = isExceptionWillBeHandled({ + message: (responseJson as { message: string }).message, + } as HTTPError); + + if (isShouldIgnore) return error as HTTPError; - Sentry.captureException(error, { - extra: { - ErrorMessage: `StatusCode: ${response?.status}, URL:${response?.url}`, - // @ts-ignore - reqId: response?.headers?.['X-Request-ID'], - bodyRaw: responseBody, - }, + throw error; + } catch (error) { + Sentry.withScope(scope => { + // group errors together based on their request and response + scope.setFingerprint([ + request.method, + request.url, + String((error as HTTPError).response.status), + getAccessToken() || 'anonymous', + ]); + Sentry.setUser({ + id: getAccessToken() || 'anonymous', + }); + + Sentry.captureException(error, { + extra: { + ErrorMessage: `StatusCode: ${response?.status}, URL:${response?.url}`, + // @ts-ignore + reqId: response?.headers?.['X-Request-ID'], + bodyRaw: responseBody, + }, + }); }); - }); - return error; + return error as HTTPError; + } }, ], }, diff --git a/apps/kyb-app/src/domains/collection-flow/collection-flow.api.ts b/apps/kyb-app/src/domains/collection-flow/collection-flow.api.ts index 508a0179e..f5cf9ec69 100644 --- a/apps/kyb-app/src/domains/collection-flow/collection-flow.api.ts +++ b/apps/kyb-app/src/domains/collection-flow/collection-flow.api.ts @@ -93,6 +93,9 @@ export const fetchFlowContext = async (): Promise => { export interface EndUser { id: string; + email: string; + firstName: string; + lastName: string; } export const fetchEndUser = async (): Promise => { diff --git a/apps/kyb-app/src/hooks/useEndUserQuery/useEndUserQuery.ts b/apps/kyb-app/src/hooks/useEndUserQuery/useEndUserQuery.ts index 6832c0f2b..fefa11674 100644 --- a/apps/kyb-app/src/hooks/useEndUserQuery/useEndUserQuery.ts +++ b/apps/kyb-app/src/hooks/useEndUserQuery/useEndUserQuery.ts @@ -1,16 +1,52 @@ import { collectionFlowQuerykeys } from '@/domains/collection-flow'; +import { + clearPostHogUser, + clearSentryUser, + updatePostHogUser, + updateSentryUser, +} from '@/initialize-monitoring/initialize-monitoring'; import { useQuery } from '@tanstack/react-query'; import { HTTPError } from 'ky'; +import { useEffect } from 'react'; export const useEndUserQuery = () => { - const { data, isLoading, error, refetch } = useQuery({ + const { + data: endUser, + isLoading, + error, + refetch, + } = useQuery({ ...collectionFlowQuerykeys.getEndUser(), // @ts-ignore staleTime: Infinity as const, }); + useEffect(() => { + if (endUser) { + updateSentryUser({ + id: endUser.id, + email: endUser.email, + fullName: `${endUser.firstName} ${endUser.lastName}`, + }); + + updatePostHogUser({ + id: endUser.id, + email: endUser.email, + fullName: `${endUser.firstName} ${endUser.lastName}`, + }); + } else { + clearSentryUser(); + clearPostHogUser(); + } + + return () => { + clearSentryUser(); + clearPostHogUser(); + }; + }, [endUser]); + return { - data, + data: endUser, isLoading, error: error ? (error as HTTPError) : null, refetch, diff --git a/apps/kyb-app/src/initialize-monitoring/initialize-monitoring.ts b/apps/kyb-app/src/initialize-monitoring/initialize-monitoring.ts index 94b18c39a..f31b3a5e2 100644 --- a/apps/kyb-app/src/initialize-monitoring/initialize-monitoring.ts +++ b/apps/kyb-app/src/initialize-monitoring/initialize-monitoring.ts @@ -49,3 +49,33 @@ export const initializeMonitoring = () => { }); } }; + +export const updateSentryUser = (userMetadata: { + id?: string; + email?: string; + fullName?: string; +}) => { + Sentry.setUser({ + id: userMetadata.id, + email: userMetadata.email, + username: userMetadata.fullName, + }); +}; + +export const updatePostHogUser = (userMetadata: { + id?: string; + email?: string; + fullName?: string; +}) => { + if (userMetadata.email) { + posthog.identify(userMetadata.email, userMetadata); + } +}; + +export const clearSentryUser = () => { + Sentry.setUser(null); +}; + +export const clearPostHogUser = () => { + posthog.reset(); +};