Skip to content

Commit 52591c7

Browse files
committed
Throw server error if translation fails
1 parent e226298 commit 52591c7

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

src/main/java/org/wise/portal/presentation/web/controllers/author/project/TranslateProjectAPIController.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ protected void saveTranslations(Authentication auth,
6262
}
6363

6464
@PostMapping("suggest")
65-
protected String getSuggestedTranslation(Authentication auth, @RequestBody TranslatableText translatableText) throws IOException, IllegalArgumentException {
65+
protected String getSuggestedTranslation(Authentication auth, @RequestBody TranslatableText translatableText)
66+
throws IOException, IllegalArgumentException, ResponseStatusException {
6667
if (accessKey.equals("") || secretKey.equals("") || region.equals("")) {
6768
throw new ResponseStatusException(
6869
HttpStatus.INTERNAL_SERVER_ERROR,
@@ -71,8 +72,7 @@ protected String getSuggestedTranslation(Authentication auth, @RequestBody Trans
7172
} else {
7273
TranslateClient translateClient = buildTranslateClient();
7374
TranslateTextRequest request = buildTranslateTextRequest(translatableText);
74-
TranslateTextResponse textResponse = translateClient.translateText(request);
75-
return textResponse.translatedText();
75+
return this.translateText(translateClient, request);
7676
}
7777
}
7878

@@ -91,4 +91,17 @@ private TranslateTextRequest buildTranslateTextRequest(TranslatableText translat
9191
.targetLanguageCode(translatableText.getTargetLangCode())
9292
.build();
9393
}
94+
95+
private String translateText(TranslateClient client, TranslateTextRequest request) throws ResponseStatusException {
96+
TranslateTextResponse textResponse;
97+
try {
98+
textResponse = client.translateText(request);
99+
} catch (Exception e) {
100+
throw new ResponseStatusException(
101+
HttpStatus.INTERNAL_SERVER_ERROR,
102+
"Translation failed"
103+
);
104+
}
105+
return textResponse.translatedText();
106+
}
94107
}

0 commit comments

Comments
 (0)