From eeacd7d6ac84afcf1efc0ee7aa3e42cc0cd91ffc Mon Sep 17 00:00:00 2001 From: Ali Ariff Date: Sat, 31 Aug 2024 15:03:54 +0200 Subject: [PATCH] refactor: Improve error handling in HtmlBeautifier class --- src/formatter/htmlbeautifier.ts | 32 +++++++------------------------- 1 file changed, 7 insertions(+), 25 deletions(-) diff --git a/src/formatter/htmlbeautifier.ts b/src/formatter/htmlbeautifier.ts index 9746eac..b0d93ff 100644 --- a/src/formatter/htmlbeautifier.ts +++ b/src/formatter/htmlbeautifier.ts @@ -75,7 +75,13 @@ 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); @@ -83,30 +89,6 @@ export default class HtmlBeautifier { }); } - /** - * 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) => 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.