Skip to content

Commit

Permalink
fix(LocalFileExtractor): log correct error message
Browse files Browse the repository at this point in the history
Problem: no matter why readFile fails, the error is reported as 'file not
found'
Solution: print the thrown errors message
  • Loading branch information
TungstnBallon committed Aug 29, 2024
1 parent d39b439 commit 7a02683
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'`,
);
}
});
Expand Down
56 changes: 30 additions & 26 deletions libs/extensions/std/exec/src/local-file-extractor-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 7a02683

Please sign in to comment.