From 7a02683ac1c4ec63b4fc6754ab3debfccd4a6468 Mon Sep 17 00:00:00 2001 From: Jonas Zeltner Date: Wed, 31 Jul 2024 16:40:46 +0200 Subject: [PATCH] fix(LocalFileExtractor): log correct error message Problem: no matter why readFile fails, the error is reported as 'file not found' Solution: print the thrown errors message --- .../src/local-file-extractor-executor.spec.ts | 2 +- .../exec/src/local-file-extractor-executor.ts | 56 ++++++++++--------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/libs/extensions/std/exec/src/local-file-extractor-executor.spec.ts b/libs/extensions/std/exec/src/local-file-extractor-executor.spec.ts index b06200362..0a259c955 100644 --- a/libs/extensions/std/exec/src/local-file-extractor-executor.spec.ts +++ b/libs/extensions/std/exec/src/local-file-extractor-executor.spec.ts @@ -104,7 +104,7 @@ describe('Validation of LocalFileExtractorExecutor', () => { expect(R.isErr(result)).toEqual(true); if (R.isErr(result)) { expect(result.left.message).toEqual( - `File './does-not-exist.csv' not found.`, + `ENOENT: no such file or directory, open './does-not-exist.csv'`, ); } }); diff --git a/libs/extensions/std/exec/src/local-file-extractor-executor.ts b/libs/extensions/std/exec/src/local-file-extractor-executor.ts index 23ea6780b..a70930d5c 100644 --- a/libs/extensions/std/exec/src/local-file-extractor-executor.ts +++ b/libs/extensions/std/exec/src/local-file-extractor-executor.ts @@ -47,36 +47,40 @@ export class LocalFileExtractorExecutor extends AbstractBlockExecutor< }); } + let rawData: Buffer | undefined = undefined; try { - const rawData = await fs.readFile(filePath); - - // Infer FileName and FileExtension from filePath - const fileName = path.basename(filePath); - const extName = path.extname(fileName); - const fileExtension = - inferFileExtensionFromFileExtensionString(extName) ?? - FileExtension.NONE; - - // Infer Mimetype from FileExtension, if not inferrable, then default to application/octet-stream - const mimeType: MimeType | undefined = - inferMimeTypeFromFileExtensionString(fileExtension) ?? - MimeType.APPLICATION_OCTET_STREAM; - - // Create file and return file - const file = new BinaryFile( - fileName, - fileExtension, - mimeType, - rawData.buffer as ArrayBuffer, - ); - - context.logger.logDebug(`Successfully extraced file ${filePath}`); - return R.ok(file); - } catch (error) { + rawData = await fs.readFile(filePath); + } catch (error: unknown) { + const message = + error instanceof Error + ? error.message + : `Could not read file ${filePath}`; return R.err({ - message: `File '${filePath}' not found.`, + message: message, diagnostic: { node: context.getCurrentNode(), property: 'filePath' }, }); } + + // Infer FileName and FileExtension from filePath + const fileName = path.basename(filePath); + const extName = path.extname(fileName); + const fileExtension = + inferFileExtensionFromFileExtensionString(extName) ?? FileExtension.NONE; + + // Infer Mimetype from FileExtension, if not inferrable, then default to application/octet-stream + const mimeType: MimeType | undefined = + inferMimeTypeFromFileExtensionString(fileExtension) ?? + MimeType.APPLICATION_OCTET_STREAM; + + // Create file and return file + const file = new BinaryFile( + fileName, + fileExtension, + mimeType, + rawData.buffer as ArrayBuffer, + ); + + context.logger.logDebug(`Successfully extraced file ${filePath}`); + return R.ok(file); } }