From 97b57af72bf020da187c086443bae457217ea77c Mon Sep 17 00:00:00 2001 From: doubleface Date: Mon, 11 Sep 2023 18:35:37 +0200 Subject: [PATCH] feat: (Clisk) Catch and log downloadFileInWorker errors If there was an error in the downloadFileInWorker function, it would cause the whole clisk konnector run to fail Now, the error is caught and a warning error will be visible in the konnector logs. This is also coherent with the behavior of node konnectors. --- src/libs/Launcher.js | 24 ++++++++++--- src/libs/Launcher.spec.js | 76 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 5 deletions(-) diff --git a/src/libs/Launcher.js b/src/libs/Launcher.js index bd207cc2e..f839832b8 100644 --- a/src/libs/Launcher.js +++ b/src/libs/Launcher.js @@ -458,11 +458,25 @@ export default class Launcher { sourceAccount: job.message.account, sourceAccountIdentifier, // @ts-ignore - downloadAndFormatFile: async entry => ({ - ...entry, - // @ts-ignore - dataUri: await this.worker.call('downloadFileInWorker', entry) - }), + downloadAndFormatFile: async entry => { + try { + // @ts-ignore + const dataUri = await this.worker.call('downloadFileInWorker', entry) + return { + ...entry, + dataUri + } + } catch (err) { + this.log({ + level: 'warning', + // @ts-ignore + message: `Could not download entry: ${entry.fileurl}: ${err.message}`, + label: 'downloadAndFormatFile', + namespace: 'Launcher' + }) + return entry + } + }, existingFilesIndex }) log.debug(result, 'saveFiles result') diff --git a/src/libs/Launcher.spec.js b/src/libs/Launcher.spec.js index 156d177a9..85c3033f4 100644 --- a/src/libs/Launcher.spec.js +++ b/src/libs/Launcher.spec.js @@ -140,8 +140,84 @@ describe('Launcher', () => { }) }) describe('saveFiles', () => { + it('should not throw if a downloadFileInWorker throws but log a warning message', async () => { + const launcher = new Launcher() + launcher.log = jest.fn() + launcher.worker = { + call: jest + .fn() + .mockRejectedValue(new Error('downloadFileInWorker error message')) + } + launcher.setUserData({ + sourceAccountIdentifier: 'testsourceaccountidentifier' + }) + const konnector = { slug: 'testkonnectorslug' } + const trigger = { + message: { + folder_to_save: 'testfolderid', + account: 'testaccountid' + } + } + const job = { + message: { account: 'testaccountid', folder_to_save: 'testfolderid' } + } + const client = { + queryAll: jest.fn().mockResolvedValue([ + { + metadata: { + fileIdAttributes: 'fileidattribute' + } + } + ]), + query: jest.fn().mockResolvedValue({ data: { path: 'folderPath' } }) + } + launcher.setStartContext({ + konnector, + client, + launcherClient: client, + trigger, + job + }) + + saveFiles.mockImplementation((client, entries, folderPath, options) => { + options.downloadAndFormatFile(entries[0]) + }) + + await launcher.saveFiles( + [ + { + fileurl: 'test file url' + } + ], + {} + ) + + expect(saveFiles).toHaveBeenCalledWith( + client, + [{ fileurl: 'test file url' }], + 'folderPath', + expect.objectContaining({ + existingFilesIndex: new Map([ + [ + 'fileidattribute', + { metadata: { fileIdAttributes: 'fileidattribute' } } + ] + ]), + sourceAccount: 'testaccountid', + sourceAccountIdentifier: 'testsourceaccountidentifier' + }) + ) + expect(launcher.log).toHaveBeenCalledWith({ + level: 'warning', + message: + 'Could not download entry: test file url: downloadFileInWorker error message', + namespace: 'Launcher', + label: 'downloadAndFormatFile' + }) + }) it('should create an index of existing files and pass it to cozy-clisk saveFiles', async () => { const launcher = new Launcher() + launcher.log = jest.fn() launcher.setUserData({ sourceAccountIdentifier: 'testsourceaccountidentifier' })