diff --git a/src/utils/getTranslatedCommitMessage.ts b/src/utils/getTranslatedCommitMessage.ts index 7c0def5..bea9855 100644 --- a/src/utils/getTranslatedCommitMessage.ts +++ b/src/utils/getTranslatedCommitMessage.ts @@ -1,5 +1,6 @@ -import * as vscode from "vscode"; +import { AxiosError } from "axios"; import { postDeeplApi } from "../api/postDeeplApi"; +import { setApiKeyCommand } from "../commands"; import { getConfiguration } from "./configuration/getConfiguration"; type getTranslatedCommitMessageProps = { @@ -13,7 +14,8 @@ export default async function getTranslatedCommitMessage({ }: getTranslatedCommitMessageProps): Promise { try { const targetLanguage = - getConfiguration().get("deepl.targetLanguage") ?? "EN"; + getConfiguration().get("deepl.targetLanguage") ?? + "EN"; const response = await postDeeplApi({ text: commit.trim(), @@ -22,11 +24,17 @@ export default async function getTranslatedCommitMessage({ }); return response.translations[0].text; } catch (error) { - if (error instanceof Error) { - vscode.window.showErrorMessage(error.message); - throw error; + if (error instanceof AxiosError && error.response?.status === 403) { + const errorMessage = "The API Key is invalid, please enter it again."; + setApiKeyCommand(); + throw new Error(errorMessage); + } else if (error instanceof Error) { + const errorMessage = "Error in deepl api: " + error.message; + throw new Error(errorMessage); } else { - throw new Error("An error occurred while translating the commit message."); + const errorMessage = + "An unexpected error occurred while translating the commit message."; + throw new Error(errorMessage); } } }