diff --git a/packages/cli/src/executionLifecycleHooks/restoreBinaryDataId.ts b/packages/cli/src/executionLifecycleHooks/restoreBinaryDataId.ts index 15c03e1b4df63..84780a785e410 100644 --- a/packages/cli/src/executionLifecycleHooks/restoreBinaryDataId.ts +++ b/packages/cli/src/executionLifecycleHooks/restoreBinaryDataId.ts @@ -3,6 +3,7 @@ import { BinaryDataService } from 'n8n-core'; import type { IRun, WorkflowExecuteMode } from 'n8n-workflow'; import type { BinaryData } from 'n8n-core'; import config from '@/config'; +import { Logger } from '@/Logger'; /** * Whenever the execution ID is not available to the binary data service at the @@ -32,28 +33,43 @@ export async function restoreBinaryDataId( return; } - const { runData } = run.data.resultData; + try { + const { runData } = run.data.resultData; - const promises = Object.keys(runData).map(async (nodeName) => { - const binaryDataId = runData[nodeName]?.[0]?.data?.main?.[0]?.[0]?.binary?.data?.id; + const promises = Object.keys(runData).map(async (nodeName) => { + const binaryDataId = runData[nodeName]?.[0]?.data?.main?.[0]?.[0]?.binary?.data?.id; - if (!binaryDataId) return; + if (!binaryDataId) return; - const [mode, fileId] = binaryDataId.split(':') as [BinaryData.StoredMode, string]; + const [mode, fileId] = binaryDataId.split(':') as [BinaryData.StoredMode, string]; - const isMissingExecutionId = fileId.includes('/temp/'); + const isMissingExecutionId = fileId.includes('/temp/'); - if (!isMissingExecutionId) return; + if (!isMissingExecutionId) return; - const correctFileId = fileId.replace('temp', executionId); + const correctFileId = fileId.replace('temp', executionId); - await Container.get(BinaryDataService).rename(fileId, correctFileId); + await Container.get(BinaryDataService).rename(fileId, correctFileId); - const correctBinaryDataId = `${mode}:${correctFileId}`; + const correctBinaryDataId = `${mode}:${correctFileId}`; - // @ts-expect-error Validated at the top - run.data.resultData.runData[nodeName][0].data.main[0][0].binary.data.id = correctBinaryDataId; - }); + // @ts-expect-error Validated at the top + run.data.resultData.runData[nodeName][0].data.main[0][0].binary.data.id = correctBinaryDataId; + }); - await Promise.all(promises); + await Promise.all(promises); + } catch (e) { + const error = e instanceof Error ? e : new Error(`${e}`); + const logger = Container.get(Logger); + + if (error.message.includes('ENOENT')) { + logger.warn('Failed to restore binary data ID - No such file or dir', { + executionId, + error, + }); + return; + } + + logger.error('Failed to restore binary data ID - Unknown error', { executionId, error }); + } } diff --git a/packages/cli/test/unit/execution-lifecyle/restoreBinaryDataId.test.ts b/packages/cli/test/unit/execution-lifecyle/restoreBinaryDataId.test.ts index 05d828100cbac..3fcdb79c72c7e 100644 --- a/packages/cli/test/unit/execution-lifecyle/restoreBinaryDataId.test.ts +++ b/packages/cli/test/unit/execution-lifecyle/restoreBinaryDataId.test.ts @@ -142,6 +142,28 @@ for (const mode of ['filesystem-v2', 's3'] as const) { expect(binaryDataService.rename).not.toHaveBeenCalled(); }); + + it('should ignore error thrown on renaming', async () => { + const workflowId = '6HYhhKmJch2cYxGj'; + const executionId = 'temp'; + const binaryDataFileUuid = 'a5c3f1ed-9d59-4155-bc68-9a370b3c51f6'; + + const incorrectFileId = `workflows/${workflowId}/executions/temp/binary_data/${binaryDataFileUuid}`; + + const run = toIRun({ + binary: { + data: { id: `s3:${incorrectFileId}` }, + }, + }); + + binaryDataService.rename.mockRejectedValueOnce(new Error('ENOENT')); + + const promise = restoreBinaryDataId(run, executionId, 'webhook'); + + await expect(promise).resolves.not.toThrow(); + + expect(binaryDataService.rename).toHaveBeenCalled(); + }); }); }