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

Mitigate empty output #74

Merged
merged 1 commit into from
Aug 31, 2024
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
22 changes: 17 additions & 5 deletions src/formatter/htmlbeautifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,26 @@ export default class HtmlBeautifier {
});

htmlbeautifier.on("exit", (code) => {
const errorMessage = Buffer.concat(stderrChunks).toString().trim();

// Handle non-zero exit codes as errors
if (code !== 0) {
return reject(
`Formatting failed with exit code ${code}: ${errorMessage}`
);
}

// Handle case where output is empty but input was not
const formattedResult = Buffer.concat(stdoutChunks).toString();
const finalResult = this.handleFinalNewline(input, formattedResult);
const errorMessage = Buffer.concat(stderrChunks).toString();
if (code && code !== 0) {
reject(`Failed with exit code ${code}. ${errorMessage}`);
} else {
resolve(finalResult);
if (input.trim() && finalResult.trim() === "") {
return reject(
`Formatting failed: the output is unexpectedly empty despite non-empty input. ${errorMessage}`
);
}

// If no errors, resolve with the formatted result
resolve(finalResult);
});

htmlbeautifier.stdin.write(input);
Expand Down