Skip to content

Commit deb19da

Browse files
authored
fix: handle 403 error in getTranslatedCommitMessage (#6)
1 parent a5d7688 commit deb19da

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/utils/getTranslatedCommitMessage.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import * as vscode from "vscode";
1+
import { AxiosError } from "axios";
22
import { postDeeplApi } from "../api/postDeeplApi";
3+
import { setApiKeyCommand } from "../commands";
34
import { getConfiguration } from "./configuration/getConfiguration";
45

56
type getTranslatedCommitMessageProps = {
@@ -13,7 +14,8 @@ export default async function getTranslatedCommitMessage({
1314
}: getTranslatedCommitMessageProps): Promise<string> {
1415
try {
1516
const targetLanguage =
16-
getConfiguration().get<string | undefined>("deepl.targetLanguage") ?? "EN";
17+
getConfiguration().get<string | undefined>("deepl.targetLanguage") ??
18+
"EN";
1719

1820
const response = await postDeeplApi({
1921
text: commit.trim(),
@@ -22,11 +24,17 @@ export default async function getTranslatedCommitMessage({
2224
});
2325
return response.translations[0].text;
2426
} catch (error) {
25-
if (error instanceof Error) {
26-
vscode.window.showErrorMessage(error.message);
27-
throw error;
27+
if (error instanceof AxiosError && error.response?.status === 403) {
28+
const errorMessage = "The API Key is invalid, please enter it again.";
29+
setApiKeyCommand();
30+
throw new Error(errorMessage);
31+
} else if (error instanceof Error) {
32+
const errorMessage = "Error in deepl api: " + error.message;
33+
throw new Error(errorMessage);
2834
} else {
29-
throw new Error("An error occurred while translating the commit message.");
35+
const errorMessage =
36+
"An unexpected error occurred while translating the commit message.";
37+
throw new Error(errorMessage);
3038
}
3139
}
3240
}

0 commit comments

Comments
 (0)