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

refactor(index): remove catch rethrowing errors #241

Merged
merged 1 commit into from
Jan 13, 2023
Merged
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
115 changes: 54 additions & 61 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,74 +150,67 @@ class UnRTF {
quiet: { arg: "--quiet", type: "boolean", minVersion: "0.21.3" },
};

try {
/**
* UnRTF will attempt to convert empty strings/files, and non-RTF files
* so catch them here
*/
if (
file === undefined ||
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.existsSync(path.normalizeTrim(file)) === false
) {
throw new Error("File missing");
}

const results = await fileType.fromFile(path.normalizeTrim(file));
if (
results === undefined ||
results.mime === undefined ||
results.mime !== "application/rtf"
) {
throw new Error(
"File is not the correct media type, expected 'application/rtf'"
);
}
/**
* UnRTF will attempt to convert empty strings/files, and non-RTF files
* so catch them here
*/
if (
file === undefined ||
// eslint-disable-next-line security/detect-non-literal-fs-filename
fs.existsSync(path.normalizeTrim(file)) === false
) {
throw new Error("File missing");
}

const { stderr } = await execFileAsync(
path.joinSafe(this.unrtfPath, "unrtf"),
["--version"]
const results = await fileType.fromFile(path.normalizeTrim(file));
if (
results === undefined ||
results.mime === undefined ||
results.mime !== "application/rtf"
) {
throw new Error(
"File is not the correct media type, expected 'application/rtf'"
);
}

/**
* UnRTF outputs the version into stderr:
* v0.19.3 returns "0.19.3\r\n"
* v0.21.0 returns "0.21.10\nsearch path is: /usr/share/unrtf/\n"
*/
const versionInfo = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/i.exec(stderr)[1];
const { stderr } = await execFileAsync(
path.joinSafe(this.unrtfPath, "unrtf"),
["--version"]
);

const args = parseOptions(acceptedOptions, options, versionInfo);
args.push(path.normalizeTrim(file));
/**
* UnRTF outputs the version into stderr:
* v0.19.3 returns "0.19.3\r\n"
* v0.21.0 returns "0.21.10\nsearch path is: /usr/share/unrtf/\n"
*/
const versionInfo = /^(\d{1,2}\.\d{1,2}\.\d{1,2})/i.exec(stderr)[1];

return new Promise((resolve, reject) => {
const child = spawn(
path.joinSafe(this.unrtfPath, "unrtf"),
args
);
const args = parseOptions(acceptedOptions, options, versionInfo);
args.push(path.normalizeTrim(file));

return new Promise((resolve, reject) => {
const child = spawn(path.joinSafe(this.unrtfPath, "unrtf"), args);

let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
stdOut += data;
});

child.stderr.on("data", async (data) => {
stdErr += data;
});

child.on("close", async () => {
/* istanbul ignore else */
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
let stdOut = "";
let stdErr = "";

child.stdout.on("data", async (data) => {
stdOut += data;
});
} catch (err) {
return Promise.reject(err);
}

child.stderr.on("data", async (data) => {
stdErr += data;
});

child.on("close", async () => {
/* istanbul ignore else */
if (stdOut !== "") {
resolve(stdOut.trim());
} else {
reject(new Error(stdErr.trim()));
}
});
});
}
}

Expand Down