@@ -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