Skip to content

Commit

Permalink
fix: handle 403 error in getTranslatedCommitMessage (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
minai621 authored Dec 5, 2023
1 parent a5d7688 commit deb19da
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/utils/getTranslatedCommitMessage.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -13,7 +14,8 @@ export default async function getTranslatedCommitMessage({
}: getTranslatedCommitMessageProps): Promise<string> {
try {
const targetLanguage =
getConfiguration().get<string | undefined>("deepl.targetLanguage") ?? "EN";
getConfiguration().get<string | undefined>("deepl.targetLanguage") ??
"EN";

const response = await postDeeplApi({
text: commit.trim(),
Expand All @@ -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);
}
}
}

0 comments on commit deb19da

Please sign in to comment.