From 3c89a699f84e7ca6c86c1858d6251f315ae40d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20S=C3=A9ghair?= Date: Fri, 17 Jan 2025 16:15:17 +0100 Subject: [PATCH] chore: Do not send 403 errors to Sentry in verification tag retrieval (#1842) * chore: Update verification tag retrieval tests to enhance error handling * chore: update PHP-CS-Fixer action in GitHub workflow --- .../src/verification-tag-retriever.spec.ts | 60 ++++++++++++++++++- .../src/verification-tag-retriever.ts | 11 ++-- 2 files changed, 66 insertions(+), 5 deletions(-) diff --git a/_dev/apps/verification-tag/src/verification-tag-retriever.spec.ts b/_dev/apps/verification-tag/src/verification-tag-retriever.spec.ts index 6eb5823c62..1a03ae87bc 100644 --- a/_dev/apps/verification-tag/src/verification-tag-retriever.spec.ts +++ b/_dev/apps/verification-tag/src/verification-tag-retriever.spec.ts @@ -1,7 +1,14 @@ import { HttpClientError } from "mktg-with-google-common"; import { runRetrievalOfVerificationTag } from "./verification-tag-retriever"; +import * as Sentry from '@sentry/browser'; + +jest.mock('@sentry/browser'); describe('runRetrievalOfVerificationTag', () => { + beforeEach(() => { + jest.clearAllMocks(); // Clear any previous mock calls + }); + it('runs the whole process', async () => { // prepare const fetchOnboardingMock = jest.fn(); @@ -84,4 +91,55 @@ describe('runRetrievalOfVerificationTag', () => { expect(fetchOnboardingMock).toBeCalledTimes(1); expect(fetchShopMock).toBeCalledTimes(0); }); -}); \ No newline at end of file + + it('handles 403 Forbidden error gracefully', async () => { + // prepare + const fetchOnboardingMock = jest.fn(); + const fetchShopMock = jest.fn(); + fetchOnboardingMock.mockImplementationOnce(() => { + throw new HttpClientError("subscription not found", 403); + }); + + // act + await runRetrievalOfVerificationTag(fetchOnboardingMock, fetchShopMock); + + // assert + expect(fetchOnboardingMock).toBeCalledTimes(1); + expect(fetchShopMock).toBeCalledTimes(0); + expect(Sentry.captureException).not.toHaveBeenCalled(); // Ensure Sentry is not called + }); + + it('handles 404 Not Found error', async () => { + // prepare + const fetchOnboardingMock = jest.fn(); + const fetchShopMock = jest.fn(); + fetchOnboardingMock.mockImplementationOnce(() => { + throw new HttpClientError("oh no", 404); + }); + + // act + await runRetrievalOfVerificationTag(fetchOnboardingMock, fetchShopMock); + + // assert + expect(fetchOnboardingMock).toBeCalledTimes(1); + expect(fetchShopMock).toBeCalledTimes(0); + expect(Sentry.captureException).toHaveBeenCalled(); // Ensure Sentry is called + }); + + it('stops when token retrieval fails', async () => { + // prepare + const fetchOnboardingMock = jest.fn(); + const fetchShopMock = jest.fn(); + fetchOnboardingMock.mockImplementationOnce(() => { + throw new HttpClientError("Token retrieval failed", 500); + }); + + // act + await runRetrievalOfVerificationTag(fetchOnboardingMock, fetchShopMock); + + // assert + expect(fetchOnboardingMock).toBeCalledTimes(1); + expect(fetchShopMock).toBeCalledTimes(0); + expect(Sentry.captureException).toHaveBeenCalled(); // Ensure Sentry is called + }); +}); diff --git a/_dev/apps/verification-tag/src/verification-tag-retriever.ts b/_dev/apps/verification-tag/src/verification-tag-retriever.ts index 5ebf402ee2..2377f9c7b5 100644 --- a/_dev/apps/verification-tag/src/verification-tag-retriever.ts +++ b/_dev/apps/verification-tag/src/verification-tag-retriever.ts @@ -22,7 +22,7 @@ export const runRetrievalOfVerificationTag = async ( onResponse: responseHandler, }, )).json(); - + // 2- Store token in shop await fetchShop("setWebsiteVerificationMeta", { websiteVerificationMeta: token, @@ -48,14 +48,18 @@ export const runRetrievalOfVerificationTag = async ( onResponse: responseHandler, }, ); - + console.info('Marketing with Google - Google Verification tag has been refreshed.'); analytics?.track('[GGL] Re-verification & claiming Succeeded'); } catch (e) { console.error('Marketing with Google - Google Verification tag refresh failed.', e); analytics?.track('[GGL] Re-verification & claiming Failed'); scope.setTag('correlationId', correlationId); - Sentry.captureException(e, scope); + + // Send error to Sentry if it's not a 403 Forbidden error + if (e.code !== 403) { + Sentry.captureException(e, scope); + } } }; @@ -82,4 +86,3 @@ const responseHandler = async (response: Response) => { } return response; }; -