Skip to content

Commit b3c47ca

Browse files
authored
Merge pull request #333 from shelfio/feature/LOAPI-19-re-throw
LOAPI-19 Rethrow the error if toString throws Cannot read properties of null.
2 parents b3c9fcf + bbaae4c commit b3c47ca

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/convert.ts

+16-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import childProcess from 'child_process';
22
import util from 'util';
3+
import path from 'node:path';
34
import {cleanupTempFiles} from './cleanup';
45
import {getConvertedFilePath} from './logs';
56

@@ -25,16 +26,27 @@ export async function convertTo(filename: string, format: string): Promise<strin
2526
const cmd = `cd /tmp && ${LO_BINARY_PATH} ${argumentsString} --convert-to ${format} --outdir /tmp '/tmp/${outputFilename}'`;
2627

2728
let logs;
29+
let err;
2830

2931
// due to an unknown issue, we need to run command twice
3032
try {
31-
logs = (await exec(cmd)).stdout;
33+
const {stdout, stderr} = await exec(cmd);
34+
logs = stdout;
35+
err = stderr;
3236
} catch (e) {
33-
logs = (await exec(cmd)).stdout;
37+
const {stdout, stderr} = await exec(cmd);
38+
logs = stdout;
39+
err = stderr;
40+
} finally {
41+
await exec(`rm '/tmp/${outputFilename}'`);
42+
await cleanupTempFiles();
3443
}
3544

36-
await exec(`rm '/tmp/${outputFilename}'`);
37-
await cleanupTempFiles();
45+
if (err) {
46+
throw new Error(`Cannot generate PDF preview for .${path.extname(outputFilename)} file`, {
47+
cause: logs,
48+
});
49+
}
3850

3951
return getConvertedFilePath(logs.toString());
4052
}

0 commit comments

Comments
 (0)