Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: (Clisk) Catch and log downloadFileInWorker errors #927

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/libs/Launcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
76 changes: 76 additions & 0 deletions src/libs/Launcher.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
Expand Down
Loading