Skip to content

Commit 118c87b

Browse files
committed
Fix sentry error on consent
1 parent 20cf663 commit 118c87b

4 files changed

Lines changed: 68 additions & 2 deletions

File tree

src/components/CallToAction/Newsletter/client/__tests__/index.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ describe.each(newsletterVariants)('NewsletterFormElement web component (%s)', va
211211

212212
expect(newsletterSubscribeMock).toHaveBeenCalledWith({
213213
email: 'test@example.com',
214-
website_url: 'https://spam.example',
214+
'website_url': 'https://spam.example',
215215
consentGiven: true,
216216
})
217217
})

src/components/CallToAction/Newsletter/client/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ export class NewsletterFormElement extends LitElement {
265265
try {
266266
result = await actions.newsletter.subscribe({
267267
email,
268-
...(websiteUrl ? { website_url: websiteUrl } : {}),
268+
...(websiteUrl ? { 'website_url': websiteUrl } : {}),
269269
consentGiven,
270270
...(DataSubjectId ? { DataSubjectId } : {}),
271271
})

src/components/scripts/store/__tests__/consent.spec.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,53 @@ describe('Consent side effects', () => {
706706
expect(handleScriptErrorSpy).not.toHaveBeenCalled()
707707
})
708708

709+
it('suppresses consent transport failures without reporting a script error', async () => {
710+
vi.useFakeTimers()
711+
712+
consentCreateMock.mockRejectedValueOnce(new TypeError('Failed to fetch'))
713+
714+
const handleScriptErrorSpy = vi.spyOn(errorHandlerModule, 'handleScriptError')
715+
716+
let consentListener:
717+
| ((_state: ConsentState, _oldState?: ConsentState) => Promise<void> | void)
718+
| undefined
719+
vi.spyOn($consent, 'subscribe').mockImplementation(listener => {
720+
consentListener = listener
721+
return () => {}
722+
})
723+
vi.spyOn($isConsentBannerVisible, 'subscribe').mockImplementation(() => () => {})
724+
vi.spyOn($hasFunctionalConsent, 'subscribe').mockImplementation(() => () => {})
725+
vi.spyOn($hasAnalyticsConsent, 'subscribe').mockImplementation(() => () => {})
726+
727+
initConsentSideEffects()
728+
729+
const oldState = {
730+
analytics: false,
731+
marketing: false,
732+
functional: false,
733+
DataSubjectId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
734+
}
735+
const newState = {
736+
analytics: true,
737+
marketing: false,
738+
functional: false,
739+
DataSubjectId: 'a1b2c3d4-e5f6-7890-abcd-ef1234567890',
740+
}
741+
742+
await consentListener?.(newState, oldState)
743+
744+
await vi.advanceTimersByTimeAsync(250)
745+
746+
await vi.waitFor(() => {
747+
expect(consentCreateMock).toHaveBeenCalledTimes(1)
748+
})
749+
750+
await vi.advanceTimersByTimeAsync(30_000)
751+
752+
expect(consentCreateMock).toHaveBeenCalledTimes(1)
753+
expect(handleScriptErrorSpy).not.toHaveBeenCalled()
754+
})
755+
709756
it('deletes the data subject id when functional consent is revoked', () => {
710757
let functionalListener: ((_hasConsent: boolean) => void) | undefined
711758
vi.spyOn($hasFunctionalConsent, 'subscribe').mockImplementation(listener => {

src/components/scripts/store/consent.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,21 @@ export function initConsentSideEffects(): void {
555555
return hasCheckpointMarkup
556556
}
557557

558+
const isConsentTransportError = (error?: ConsentActionError): boolean => {
559+
const normalizedMessage = error?.message?.toLowerCase()
560+
const causeRecord = getErrorRecord(error?.cause)
561+
const causeName =
562+
typeof causeRecord?.['name'] === 'string' ? causeRecord['name'].toLowerCase() : undefined
563+
564+
return Boolean(
565+
causeName === 'aborterror' ||
566+
normalizedMessage?.includes('failed to fetch') ||
567+
normalizedMessage?.includes('load failed') ||
568+
normalizedMessage?.includes('networkerror when attempting to fetch resource') ||
569+
normalizedMessage?.includes('the internet connection appears to be offline')
570+
)
571+
}
572+
558573
const ensureOnlineListener = () => {
559574
if (typeof window === 'undefined' || onlineListener) {
560575
return
@@ -667,6 +682,10 @@ export function initConsentSideEffects(): void {
667682
return
668683
}
669684

685+
if (isConsentTransportError(actionError)) {
686+
return
687+
}
688+
670689
throw new ClientScriptError({
671690
message: serverMessage ?? 'Failed to record consent',
672691
cause: {

0 commit comments

Comments
 (0)