Clean-up try/catch blocks in translate.js

09dd9762f78d4758ed89a5dfa525d2f4471d8fc3

Cohee <18619528+Cohee1207@users.noreply.github.com>

1 files changed, +4 -14Showing whitespace changes
src/endpoints/translate.js+4 -14
@@ -41,6 +41,7 @@ function decodeBuffer(buffer) {
4141}
4242
4343router.post('/libre', jsonParser, async (request, response) => {
44+ try {
4445 const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
4546 const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
4647
@@ -70,7 +71,6 @@ router.post('/libre', jsonParser, async (request, response) => {
7071
7172 console.log('Input text: ' + text);
7273
73- try {
7474 const result = await fetch(url, {
7575 method: 'POST',
7676 body: JSON.stringify({
@@ -198,11 +198,6 @@ router.post('/lingva', jsonParser, async (request, response) => {
198198 console.log('Lingva URL is using default value.', LINGVA_DEFAULT);
199199 }
200200
201- if (!baseUrl) {
202- console.log('Lingva URL is not configured.');
203- return response.sendStatus(400);
204- }
205-
206201 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
207202 request.body.lang = 'zh';
208203 }
@@ -220,7 +215,6 @@ router.post('/lingva', jsonParser, async (request, response) => {
220215
221216 console.log('Input text: ' + text);
222217
223- try {
224218 const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text));
225219 const result = await fetch(url);
226220
@@ -234,16 +228,13 @@ router.post('/lingva', jsonParser, async (request, response) => {
234228 console.log('Translated text: ' + data.translation);
235229 return response.send(data.translation);
236230 } catch (error) {
237- console.log('Translation error:', error);
238- return response.sendStatus(500);
239- }
240- } catch (error) {
241231 console.log('Translation error', error);
242232 return response.sendStatus(500);
243233 }
244234});
245235
246236router.post('/deepl', jsonParser, async (request, response) => {
237+ try {
247238 const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL);
248239
249240 if (!key) {
@@ -273,7 +264,6 @@ router.post('/deepl', jsonParser, async (request, response) => {
273264 params.append('formality', formality);
274265 }
275266
276- try {
277267 const result = await fetch('https://api-free.deepl.com/v2/translate', {
278268 method: 'POST',
279269 body: params,
@@ -302,6 +292,7 @@ router.post('/deepl', jsonParser, async (request, response) => {
302292});
303293
304294router.post('/onering', jsonParser, async (request, response) => {
295+ try {
305296 const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL);
306297 const url = secretUrl || ONERING_URL_DEFAULT;
307298
@@ -333,7 +324,6 @@ router.post('/onering', jsonParser, async (request, response) => {
333324
334325 console.log('Input text: ' + text);
335326
336- try {
337327 const fetchUrl = new URL(url);
338328 fetchUrl.search = params.toString();
339329
@@ -359,6 +349,7 @@ router.post('/onering', jsonParser, async (request, response) => {
359349});
360350
361351router.post('/deeplx', jsonParser, async (request, response) => {
352+ try {
362353 const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL);
363354 const url = secretUrl || DEEPLX_URL_DEFAULT;
364355
@@ -383,7 +374,6 @@ router.post('/deeplx', jsonParser, async (request, response) => {
383374
384375 console.log('Input text: ' + text);
385376
386- try {
387377 const result = await fetch(url, {
388378 method: 'POST',
389379 body: JSON.stringify({