Skip to content

Commit bbaae4c

Browse files
committed
feat: LOAPI-19 rethrow via stderr
1 parent 2bc91b8 commit bbaae4c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/convert.ts

Lines changed: 11 additions & 7 deletions
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,21 +26,24 @@ 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 {
3133
const {stdout, stderr} = await exec(cmd);
32-
logs = stdout || new Error(stderr);
34+
logs = stdout;
35+
err = stderr;
3336
} catch (e) {
3437
const {stdout, stderr} = await exec(cmd);
35-
logs = stdout !== '' ? stdout : new Error(stderr);
38+
logs = stdout;
39+
err = stderr;
40+
} finally {
41+
await exec(`rm '/tmp/${outputFilename}'`);
42+
await cleanupTempFiles();
3643
}
3744

38-
await exec(`rm '/tmp/${outputFilename}'`);
39-
await cleanupTempFiles();
40-
41-
if (logs instanceof Error) {
42-
throw new Error(`Cannot generate PDF preview for .${outputFilename.split('.').pop()} file`, {
45+
if (err) {
46+
throw new Error(`Cannot generate PDF preview for .${path.extname(outputFilename)} file`, {
4347
cause: logs,
4448
});
4549
}

0 commit comments

Comments
 (0)