1
- import * as vscode from "vscode " ;
1
+ import { AxiosError } from "axios " ;
2
2
import { postDeeplApi } from "../api/postDeeplApi" ;
3
+ import { setApiKeyCommand } from "../commands" ;
3
4
import { getConfiguration } from "./configuration/getConfiguration" ;
4
5
5
6
type getTranslatedCommitMessageProps = {
@@ -13,7 +14,8 @@ export default async function getTranslatedCommitMessage({
13
14
} : getTranslatedCommitMessageProps ) : Promise < string > {
14
15
try {
15
16
const targetLanguage =
16
- getConfiguration ( ) . get < string | undefined > ( "deepl.targetLanguage" ) ?? "EN" ;
17
+ getConfiguration ( ) . get < string | undefined > ( "deepl.targetLanguage" ) ??
18
+ "EN" ;
17
19
18
20
const response = await postDeeplApi ( {
19
21
text : commit . trim ( ) ,
@@ -22,11 +24,17 @@ export default async function getTranslatedCommitMessage({
22
24
} ) ;
23
25
return response . translations [ 0 ] . text ;
24
26
} 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 ) ;
28
34
} 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 ) ;
30
38
}
31
39
}
32
40
}
0 commit comments