Skip to content

Commit e56acdc

Browse files
saikumarrsCopilot
andauthored
fix: error handle cookie operation (#2475)
* fix: error handle cookie operation * chore: update comment Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * fix: clean cookie in catch block --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5c8f156 commit e56acdc

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

  • packages/analytics-js/src/services/StoreManager/top-domain

packages/analytics-js/src/services/StoreManager/top-domain/index.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,24 @@ const domain = (url: string): string => {
6262
domain: `${domain.indexOf('localhost') !== -1 ? '' : '.'}${domain}`,
6363
};
6464

65-
// Set cookie on domain
66-
cookie(cname, 1, opts);
65+
try {
66+
// Set cookie on domain
67+
cookie(cname, 1, opts);
6768

68-
// If successful
69-
if (cookie(cname)) {
70-
// Remove cookie from domain
71-
cookie(cname, null, opts);
72-
return domain;
69+
// If successful
70+
if (cookie(cname)) {
71+
// Remove cookie from domain
72+
cookie(cname, null, opts);
73+
return domain;
74+
}
75+
} catch {
76+
// Silently continue to next domain level if cookie access is restricted or setting fails
77+
// Best-effort cleanup to avoid leaking the test cookie
78+
try {
79+
cookie(cname, null, opts);
80+
} catch {
81+
// Ignore if we are unable to delete the cookie
82+
}
7383
}
7484
}
7585

0 commit comments

Comments
 (0)