Skip to content

Commit

Permalink
feat: added skip for handled response exceptions & updated sentry met…
Browse files Browse the repository at this point in the history
…adata
  • Loading branch information
chesterkmr committed Nov 21, 2024
1 parent c25d4a3 commit 18e0fc6
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { isExceptionWillBeHandled } from '@/common/utils/helpers';
import { HTTPError } from 'ky';

interface IErrorBody {
Expand All @@ -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));
};
5 changes: 5 additions & 0 deletions apps/kyb-app/src/common/utils/helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { HTTPError } from 'ky';

export const isExceptionWillBeHandled = (error: HTTPError) => {
return error.message === 'No EndUser is set for this token';
};
54 changes: 31 additions & 23 deletions apps/kyb-app/src/common/utils/request.ts
Original file line number Diff line number Diff line change
@@ -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/`,
Expand All @@ -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;
}
},
],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ export const fetchFlowContext = async (): Promise<FlowContextResponse> => {

export interface EndUser {
id: string;
email: string;
firstName: string;
lastName: string;
}

export const fetchEndUser = async (): Promise<EndUser> => {
Expand Down
40 changes: 38 additions & 2 deletions apps/kyb-app/src/hooks/useEndUserQuery/useEndUserQuery.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
30 changes: 30 additions & 0 deletions apps/kyb-app/src/initialize-monitoring/initialize-monitoring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
};

0 comments on commit 18e0fc6

Please sign in to comment.