Skip to content

Commit

Permalink
refactor: Improve error handling in HtmlBeautifier class
Browse files Browse the repository at this point in the history
  • Loading branch information
aliariff committed Aug 31, 2024
1 parent ea688ad commit eeacd7d
Showing 1 changed file with 7 additions and 25 deletions.
32 changes: 7 additions & 25 deletions src/formatter/htmlbeautifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,38 +75,20 @@ export default class HtmlBeautifier {
const formattedResult = Buffer.concat(stdoutChunks).toString();
const finalResult = this.handleFinalNewline(input, formattedResult);
const errorMessage = Buffer.concat(stderrChunks).toString();
this.handleExit(code, finalResult, errorMessage, resolve, reject);
if (code && code !== 0) {
const error = `Failed with exit code ${code}. ${errorMessage}`;
this.handleError(error);
reject(error);
} else {
resolve(finalResult);
}
});

htmlbeautifier.stdin.write(input);
htmlbeautifier.stdin.end();
});
}

/**
* Handles the process exit event and resolves or rejects the promise.
* @param code The process exit code.
* @param result The formatted result.
* @param errorMessage The error message, if any.
* @param resolve The promise resolve function.
* @param reject The promise reject function.
*/
private handleExit(
code: number | null,
result: string,
errorMessage: string,
resolve: (value: string | PromiseLike<string>) => void,
reject: (reason?: any) => void
): void {
if (code && code !== 0) {
const error = `Failed with exit code ${code}. ${errorMessage}`;
this.handleError(error);
reject(error);
} else {
resolve(result);
}
}

/**
* Handles errors by logging and displaying a message to the user.
* @param error The error object or message.
Expand Down

0 comments on commit eeacd7d

Please sign in to comment.