From 28f3ea95ee3114ef92b0eb0142bfa64f60b7501b Mon Sep 17 00:00:00 2001 From: Richard Sustek Date: Wed, 16 Oct 2024 09:00:44 +0200 Subject: [PATCH] Fix: handles exceptions when tracking events (fixes https://github.com/kontent-ai/kontent-ai-migration-toolkit/issues/14) --- lib/core/utils/global.utils.ts | 56 ++++++++++++++++++++++++++++------ lib/toolkit/export.ts | 5 +-- lib/toolkit/file.ts | 6 ++-- lib/toolkit/import.ts | 3 +- lib/toolkit/migrate.ts | 7 +++-- 5 files changed, 59 insertions(+), 18 deletions(-) diff --git a/lib/core/utils/global.utils.ts b/lib/core/utils/global.utils.ts index a5b6546..b369907 100644 --- a/lib/core/utils/global.utils.ts +++ b/lib/core/utils/global.utils.ts @@ -1,7 +1,10 @@ -import { format } from 'bytes'; import { ITrackingEventData, getTrackingService } from '@kontent-ai-consulting/tools-analytics'; import { isBrowser, isNode, isWebWorker } from 'browser-or-node'; +import { format } from 'bytes'; +import { getDefaultLogger } from '../logs/loggers.js'; import { EnvContext } from '../models/core.models.js'; +import { Logger } from '../models/log.models.js'; +import { extractErrorData } from './error.utils.js'; export type Writeable = { -readonly [P in keyof T]: T[P] }; @@ -35,25 +38,58 @@ export const defaultZipFilename: string = 'data.zip'; export async function executeWithTrackingAsync(data: { func: () => Promise>; event: Readonly; + logger?: Logger; }): Promise> { const trackingService = getTrackingService(); - const event = await trackingService.trackEventAsync(data.event); + const logger = data.logger ?? getDefaultLogger(); + + const event = await runTrackingFuncWithErrorHadlingAsync({ + func: async () => { + return await trackingService.trackEventAsync(data.event); + }, + logger: logger + }); try { const result = await data.func(); - await trackingService.setEventResultAsync({ - eventId: event.eventId, - result: 'success' - }); + if (event) { + await runTrackingFuncWithErrorHadlingAsync({ + func: async () => { + await trackingService.setEventResultAsync({ + eventId: event.eventId, + result: 'success' + }); + }, + logger: logger + }); + } return result; } catch (error) { - await trackingService.setEventResultAsync({ - eventId: event.eventId, - result: 'fail' - }); + if (event) { + await runTrackingFuncWithErrorHadlingAsync({ + func: async () => { + await trackingService.setEventResultAsync({ + eventId: event.eventId, + result: 'fail' + }); + }, + logger: logger + }); + } throw error; } } + +async function runTrackingFuncWithErrorHadlingAsync(data: { func: () => Promise; logger: Logger }): Promise { + try { + return await data.func(); + } catch (trackingError) { + data.logger.log({ + message: `Failed to track event. ${extractErrorData(trackingError).message}`, + type: 'warning' + }); + } +} diff --git a/lib/toolkit/export.ts b/lib/toolkit/export.ts index 36336b5..5e261e2 100644 --- a/lib/toolkit/export.ts +++ b/lib/toolkit/export.ts @@ -1,6 +1,6 @@ -import { libMetadata } from '../metadata.js'; import { MigrationData, executeWithTrackingAsync } from '../core/index.js'; import { ExportConfig, exportManager } from '../export/index.js'; +import { libMetadata } from '../metadata.js'; export async function exportAsync(config: ExportConfig): Promise { return await executeWithTrackingAsync({ @@ -16,6 +16,7 @@ export async function exportAsync(config: ExportConfig): Promise }, func: async () => { return await exportManager(config).exportAsync(); - } + }, + logger: config.logger }); } diff --git a/lib/toolkit/file.ts b/lib/toolkit/file.ts index 15c3322..a0e8447 100644 --- a/lib/toolkit/file.ts +++ b/lib/toolkit/file.ts @@ -38,7 +38,8 @@ export async function storeAsync(config: StoreConfig): Promise { } else { throw Error(`Cannot store '${filename}' on File system because the provided zip is not a Buffer`); } - } + }, + logger: config.logger }); } @@ -60,6 +61,7 @@ export async function extractAsync(config: ExtractConfig): Promise { const fileData = await fileManager(logger).loadFileAsync(filename); return await zipManager(logger).parseZipAsync(fileData); - } + }, + logger: config.logger }); } diff --git a/lib/toolkit/import.ts b/lib/toolkit/import.ts index b5c9013..35d590d 100644 --- a/lib/toolkit/import.ts +++ b/lib/toolkit/import.ts @@ -16,6 +16,7 @@ export async function importAsync(config: ImportConfig): Promise { }, func: async () => { return await importManager(config).importAsync(); - } + }, + logger: config.logger }); } diff --git a/lib/toolkit/migrate.ts b/lib/toolkit/migrate.ts index 997dfc8..77cfebb 100644 --- a/lib/toolkit/migrate.ts +++ b/lib/toolkit/migrate.ts @@ -1,4 +1,3 @@ -import { libMetadata } from '../metadata.js'; import { IRetryStrategyOptions } from '@kontent-ai/core-sdk'; import { ExternalIdGenerator, @@ -9,9 +8,10 @@ import { getDefaultLogger } from '../core/index.js'; import { SourceExportItem } from '../export/index.js'; +import { ImportResult } from '../import/index.js'; +import { libMetadata } from '../metadata.js'; import { exportAsync } from './export.js'; import { importAsync } from './import.js'; -import { ImportResult } from '../import/index.js'; export interface MigrationSource extends ManagementClientConfig { readonly items: readonly SourceExportItem[]; @@ -64,6 +64,7 @@ export async function migrateAsync(config: MigrationConfig): Promise