Skip to content

Commit

Permalink
Fix: handles exceptions when tracking events (fixes #14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Oct 16, 2024
1 parent 89a39c2 commit 28f3ea9
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 18 deletions.
56 changes: 46 additions & 10 deletions lib/core/utils/global.utils.ts
Original file line number Diff line number Diff line change
@@ -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<T> = { -readonly [P in keyof T]: T[P] };

Expand Down Expand Up @@ -35,25 +38,58 @@ export const defaultZipFilename: string = 'data.zip';
export async function executeWithTrackingAsync<TResult>(data: {
func: () => Promise<TResult extends void ? void : Readonly<TResult>>;
event: Readonly<ITrackingEventData>;
logger?: Logger;
}): Promise<TResult extends void ? void : Readonly<TResult>> {
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<T>(data: { func: () => Promise<T>; logger: Logger }): Promise<T | void> {
try {
return await data.func();
} catch (trackingError) {
data.logger.log({
message: `Failed to track event. ${extractErrorData(trackingError).message}`,
type: 'warning'
});
}
}
5 changes: 3 additions & 2 deletions lib/toolkit/export.ts
Original file line number Diff line number Diff line change
@@ -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<MigrationData> {
return await executeWithTrackingAsync({
Expand All @@ -16,6 +16,7 @@ export async function exportAsync(config: ExportConfig): Promise<MigrationData>
},
func: async () => {
return await exportManager(config).exportAsync();
}
},
logger: config.logger
});
}
6 changes: 4 additions & 2 deletions lib/toolkit/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ export async function storeAsync(config: StoreConfig): Promise<void> {
} else {
throw Error(`Cannot store '${filename}' on File system because the provided zip is not a Buffer`);
}
}
},
logger: config.logger
});
}

Expand All @@ -60,6 +61,7 @@ export async function extractAsync(config: ExtractConfig): Promise<MigrationData
func: async () => {
const fileData = await fileManager(logger).loadFileAsync(filename);
return await zipManager(logger).parseZipAsync(fileData);
}
},
logger: config.logger
});
}
3 changes: 2 additions & 1 deletion lib/toolkit/import.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export async function importAsync(config: ImportConfig): Promise<ImportResult> {
},
func: async () => {
return await importManager(config).importAsync();
}
},
logger: config.logger
});
}
7 changes: 4 additions & 3 deletions lib/toolkit/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { libMetadata } from '../metadata.js';
import { IRetryStrategyOptions } from '@kontent-ai/core-sdk';
import {
ExternalIdGenerator,
Expand All @@ -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[];
Expand Down Expand Up @@ -64,6 +64,7 @@ export async function migrateAsync(config: MigrationConfig): Promise<MigrationRe
importResult,
migrationData
};
}
},
logger: config.logger
});
}

0 comments on commit 28f3ea9

Please sign in to comment.