Skip to content

Commit 2c2d771

Browse files
authored
refactor: Handle empty output (#74)
1 parent 0a93c31 commit 2c2d771

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

src/formatter/htmlbeautifier.ts

+17-5
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,26 @@ export default class HtmlBeautifier {
6262
});
6363

6464
htmlbeautifier.on("exit", (code) => {
65+
const errorMessage = Buffer.concat(stderrChunks).toString().trim();
66+
67+
// Handle non-zero exit codes as errors
68+
if (code !== 0) {
69+
return reject(
70+
`Formatting failed with exit code ${code}: ${errorMessage}`
71+
);
72+
}
73+
74+
// Handle case where output is empty but input was not
6575
const formattedResult = Buffer.concat(stdoutChunks).toString();
6676
const finalResult = this.handleFinalNewline(input, formattedResult);
67-
const errorMessage = Buffer.concat(stderrChunks).toString();
68-
if (code && code !== 0) {
69-
reject(`Failed with exit code ${code}. ${errorMessage}`);
70-
} else {
71-
resolve(finalResult);
77+
if (input.trim() && finalResult.trim() === "") {
78+
return reject(
79+
`Formatting failed: the output is unexpectedly empty despite non-empty input. ${errorMessage}`
80+
);
7281
}
82+
83+
// If no errors, resolve with the formatted result
84+
resolve(finalResult);
7385
});
7486

7587
htmlbeautifier.stdin.write(input);

0 commit comments

Comments
 (0)