From 571a542fed570761a7cf695d0d4b8feb7f54edeb Mon Sep 17 00:00:00 2001 From: Sai Kumar Battinoju Date: Fri, 21 Feb 2025 18:33:24 +0530 Subject: [PATCH] fix: address ai bot review comments --- .../storages/CookieStorage.test.ts | 15 ++++++++++++++ .../src/services/ErrorHandler/ErrorHandler.ts | 20 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/analytics-js/__tests__/services/StoreManager/storages/CookieStorage.test.ts b/packages/analytics-js/__tests__/services/StoreManager/storages/CookieStorage.test.ts index 1e1be62f2b..a7567fe6a1 100644 --- a/packages/analytics-js/__tests__/services/StoreManager/storages/CookieStorage.test.ts +++ b/packages/analytics-js/__tests__/services/StoreManager/storages/CookieStorage.test.ts @@ -31,6 +31,7 @@ describe('CookieStorage', () => { // engine.clear(); // expect(engine.length).toStrictEqual(0); }); + it('should not set domain if sameDomainCookiesOnly is set to true', () => { expect(typeof engine.options.domain).toBe('string'); configureCookieStorageEngine({ @@ -43,4 +44,18 @@ describe('CookieStorage', () => { const newEngine = getStorageEngine('cookieStorage'); expect(newEngine.options.domain).toBe(undefined); }); + + it('should properly configure cookie attributes', () => { + configureCookieStorageEngine({ + samesite: 'Strict', + secure: true, + path: '/app', + maxage: 3600000, + }); + const newEngine = getStorageEngine('cookieStorage'); + expect(newEngine.options.samesite).toBe('Strict'); + expect(newEngine.options.secure).toBe(true); + expect(newEngine.options.path).toBe('/app'); + expect(newEngine.options.maxage).toBe(3600000); + }); }); diff --git a/packages/analytics-js/src/services/ErrorHandler/ErrorHandler.ts b/packages/analytics-js/src/services/ErrorHandler/ErrorHandler.ts index 48fb16da92..433562b176 100644 --- a/packages/analytics-js/src/services/ErrorHandler/ErrorHandler.ts +++ b/packages/analytics-js/src/services/ErrorHandler/ErrorHandler.ts @@ -33,6 +33,7 @@ import { class ErrorHandler implements IErrorHandler { httpClient: IHttpClient; logger: ILogger; + private initialized = false; // If no logger is passed errors will be thrown as unhandled error constructor(httpClient: IHttpClient, logger: ILogger) { @@ -40,10 +41,22 @@ class ErrorHandler implements IErrorHandler { this.logger = logger; } + /** + * Initializes the error handler by attaching global error listeners. + * This method should be called once after construction. + */ init() { + if (this.initialized) { + return; + } + this.attachErrorListeners(); + this.initialized = true; } + /** + * Attach error listeners to the global window object + */ attachErrorListeners() { (globalThis as typeof window).addEventListener('error', (event: ErrorEvent | Event) => { this.onError(event, ERROR_HANDLER, undefined, ErrorType.UNHANDLEDEXCEPTION); @@ -57,6 +70,13 @@ class ErrorHandler implements IErrorHandler { ); } + /** + * Handle errors + * @param error - The error to handle + * @param context - The context of where the error occurred + * @param customMessage - The custom message of the error + * @param errorType - The type of the error (handled or unhandled) + */ onError( error: SDKError, context = '',