Skip to content

Commit

Permalink
refactor: Simplify 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 eeacd7d commit 9b1ca54
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 35 deletions.
37 changes: 7 additions & 30 deletions src/formatter/htmlbeautifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,11 @@ export default class HtmlBeautifier {
* @returns A promise that resolves to the formatted string.
*/
public async format(input: string): Promise<string> {
try {
const startTime = Date.now();
const result = await this.executeCommand(input);
const duration = Date.now() - startTime;
this.logChannel.info(
`Formatting completed successfully in ${duration}ms.`
);
return result;
} catch (error) {
this.handleError(error);
throw error;
}
const startTime = Date.now();
const result = await this.executeCommand(input);
const duration = Date.now() - startTime;
this.logChannel.info(`Formatting completed successfully in ${duration}ms.`);
return result;
}

/**
Expand All @@ -55,9 +48,7 @@ export default class HtmlBeautifier {
this.logChannel.info(`Formatting ERB with command: ${fullCommand}`);

if (!htmlbeautifier.stdin || !htmlbeautifier.stdout) {
const error = "Failed to spawn process, missing stdin/stdout";
this.handleError(error);
reject(error);
reject("Failed to spawn process, missing stdin/stdout");
}

const stdoutChunks: Buffer[] = [];
Expand All @@ -67,7 +58,6 @@ export default class HtmlBeautifier {
htmlbeautifier.stderr.on("data", (chunk) => stderrChunks.push(chunk));

htmlbeautifier.on("error", (error) => {
this.handleError(error);
reject(error);
});

Expand All @@ -76,9 +66,7 @@ export default class HtmlBeautifier {
const finalResult = this.handleFinalNewline(input, formattedResult);
const errorMessage = Buffer.concat(stderrChunks).toString();
if (code && code !== 0) {
const error = `Failed with exit code ${code}. ${errorMessage}`;
this.handleError(error);
reject(error);
reject(`Failed with exit code ${code}. ${errorMessage}`);
} else {
resolve(finalResult);
}
Expand All @@ -89,17 +77,6 @@ export default class HtmlBeautifier {
});
}

/**
* Handles errors by logging and displaying a message to the user.
* @param error The error object or message.
*/
private handleError(error: any): void {
const errorMessage = error instanceof Error ? error.message : String(error);
const fullMessage = `Error formatting ERB: ${errorMessage}`;
this.logChannel.error(fullMessage);
vscode.window.showErrorMessage(fullMessage);
}

/**
* Gets the executable path for HTML Beautifier based on the configuration.
* @returns The path to the executable.
Expand Down
12 changes: 7 additions & 5 deletions src/formatter/htmlbeautifierProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ export default class HtmlBeautifierProvider

return this.htmlbeautifier.format(document.getText(range)).then(
(result) => [new vscode.TextEdit(range, result)],
(err) => {
this.htmlbeautifier.logChannel.error(
`Error formatting ${document.fileName}:`,
err
);
(error) => {
const errorMessage =
error instanceof Error ? error.message : String(error);
const shortFileName = document.fileName.split("/").pop();
const fullMessage = `Error formatting ${shortFileName}: ${errorMessage}`;
this.htmlbeautifier.logChannel.error(fullMessage);
vscode.window.showErrorMessage(fullMessage);
return [];
}
);
Expand Down

0 comments on commit 9b1ca54

Please sign in to comment.