Clean-up try/catch blocks in translate.js

09dd9762f78d4758ed89a5dfa525d2f4471d8fc3

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

1 files changed, +98 -108Ignore whitespace
src/endpoints/translate.js+98 -108
@@ -41,36 +41,36 @@ function decodeBuffer(buffer) {
41}41}
4242
43router.post('/libre', jsonParser, async (request, response) => {43router.post('/libre', jsonParser, async (request, response) => {
44 const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);44 try {
45 const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);45 const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
46 const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
4647
47 if (!url) {48 if (!url) {
48 console.log('LibreTranslate URL is not configured.');49 console.log('LibreTranslate URL is not configured.');
49 return response.sendStatus(400);50 return response.sendStatus(400);
50 }51 }
5152
52 if (request.body.lang === 'zh-CN') {53 if (request.body.lang === 'zh-CN') {
53 request.body.lang = 'zh';54 request.body.lang = 'zh';
54 }55 }
5556
56 if (request.body.lang === 'zh-TW') {57 if (request.body.lang === 'zh-TW') {
57 request.body.lang = 'zt';58 request.body.lang = 'zt';
58 }59 }
5960
60 if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {61 if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
61 request.body.lang = 'pt';62 request.body.lang = 'pt';
62 }63 }
6364
64 const text = request.body.text;65 const text = request.body.text;
65 const lang = request.body.lang;66 const lang = request.body.lang;
6667
67 if (!text || !lang) {68 if (!text || !lang) {
68 return response.sendStatus(400);69 return response.sendStatus(400);
69 }70 }
7071
71 console.log('Input text: ' + text);72 console.log('Input text: ' + text);
7273
73 try {
74 const result = await fetch(url, {74 const result = await fetch(url, {
75 method: 'POST',75 method: 'POST',
76 body: JSON.stringify({76 body: JSON.stringify({
@@ -198,11 +198,6 @@ router.post('/lingva', jsonParser, async (request, response) => {
198 console.log('Lingva URL is using default value.', LINGVA_DEFAULT);198 console.log('Lingva URL is using default value.', LINGVA_DEFAULT);
199 }199 }
200200
201 if (!baseUrl) {
202 console.log('Lingva URL is not configured.');
203 return response.sendStatus(400);
204 }
205
206 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {201 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
207 request.body.lang = 'zh';202 request.body.lang = 'zh';
208 }203 }
@@ -220,23 +215,18 @@ router.post('/lingva', jsonParser, async (request, response) => {
220215
221 console.log('Input text: ' + text);216 console.log('Input text: ' + text);
222217
223 try {218 const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text));
224 const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text));219 const result = await fetch(url);
225 const result = await fetch(url);220
226221 if (!result.ok) {
227 if (!result.ok) {222 const error = await result.text();
228 const error = await result.text();223 console.log('Lingva error: ', result.statusText, error);
229 console.log('Lingva error: ', result.statusText, error);
230 }
231
232 /** @type {any} */
233 const data = await result.json();
234 console.log('Translated text: ' + data.translation);
235 return response.send(data.translation);
236 } catch (error) {
237 console.log('Translation error:', error);
238 return response.sendStatus(500);
239 }224 }
225
226 /** @type {any} */
227 const data = await result.json();
228 console.log('Translated text: ' + data.translation);
229 return response.send(data.translation);
240 } catch (error) {230 } catch (error) {
241 console.log('Translation error', error);231 console.log('Translation error', error);
242 return response.sendStatus(500);232 return response.sendStatus(500);
@@ -244,36 +234,36 @@ router.post('/lingva', jsonParser, async (request, response) => {
244});234});
245235
246router.post('/deepl', jsonParser, async (request, response) => {236router.post('/deepl', jsonParser, async (request, response) => {
247 const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL);237 try {
238 const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL);
248239
249 if (!key) {240 if (!key) {
250 console.log('DeepL key is not configured.');241 console.log('DeepL key is not configured.');
251 return response.sendStatus(400);242 return response.sendStatus(400);
252 }243 }
253244
254 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {245 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
255 request.body.lang = 'ZH';246 request.body.lang = 'ZH';
256 }247 }
257248
258 const text = request.body.text;249 const text = request.body.text;
259 const lang = request.body.lang;250 const lang = request.body.lang;
260 const formality = getConfigValue('deepl.formality', 'default');251 const formality = getConfigValue('deepl.formality', 'default');
261252
262 if (!text || !lang) {253 if (!text || !lang) {
263 return response.sendStatus(400);254 return response.sendStatus(400);
264 }255 }
265256
266 console.log('Input text: ' + text);257 console.log('Input text: ' + text);
267258
268 const params = new URLSearchParams();259 const params = new URLSearchParams();
269 params.append('text', text);260 params.append('text', text);
270 params.append('target_lang', lang);261 params.append('target_lang', lang);
271262
272 if (['de', 'fr', 'it', 'es', 'nl', 'ja', 'ru', 'pt-BR', 'pt-PT'].includes(lang)) {263 if (['de', 'fr', 'it', 'es', 'nl', 'ja', 'ru', 'pt-BR', 'pt-PT'].includes(lang)) {
273 params.append('formality', formality);264 params.append('formality', formality);
274 }265 }
275266
276 try {
277 const result = await fetch('https://api-free.deepl.com/v2/translate', {267 const result = await fetch('https://api-free.deepl.com/v2/translate', {
278 method: 'POST',268 method: 'POST',
279 body: params,269 body: params,
@@ -302,38 +292,38 @@ router.post('/deepl', jsonParser, async (request, response) => {
302});292});
303293
304router.post('/onering', jsonParser, async (request, response) => {294router.post('/onering', jsonParser, async (request, response) => {
305 const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL);295 try {
306 const url = secretUrl || ONERING_URL_DEFAULT;296 const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL);
297 const url = secretUrl || ONERING_URL_DEFAULT;
307298
308 if (!url) {299 if (!url) {
309 console.log('OneRing URL is not configured.');300 console.log('OneRing URL is not configured.');
310 return response.sendStatus(400);301 return response.sendStatus(400);
311 }302 }
312303
313 if (!secretUrl && url === ONERING_URL_DEFAULT) {304 if (!secretUrl && url === ONERING_URL_DEFAULT) {
314 console.log('OneRing URL is using default value.', ONERING_URL_DEFAULT);305 console.log('OneRing URL is using default value.', ONERING_URL_DEFAULT);
315 }306 }
316307
317 if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {308 if (request.body.lang === 'pt-BR' || request.body.lang === 'pt-PT') {
318 request.body.lang = 'pt';309 request.body.lang = 'pt';
319 }310 }
320311
321 const text = request.body.text;312 const text = request.body.text;
322 const from_lang = request.body.from_lang;313 const from_lang = request.body.from_lang;
323 const to_lang = request.body.to_lang;314 const to_lang = request.body.to_lang;
324315
325 if (!text || !from_lang || !to_lang) {316 if (!text || !from_lang || !to_lang) {
326 return response.sendStatus(400);317 return response.sendStatus(400);
327 }318 }
328319
329 const params = new URLSearchParams();320 const params = new URLSearchParams();
330 params.append('text', text);321 params.append('text', text);
331 params.append('from_lang', from_lang);322 params.append('from_lang', from_lang);
332 params.append('to_lang', to_lang);323 params.append('to_lang', to_lang);
333324
334 console.log('Input text: ' + text);325 console.log('Input text: ' + text);
335326
336 try {
337 const fetchUrl = new URL(url);327 const fetchUrl = new URL(url);
338 fetchUrl.search = params.toString();328 fetchUrl.search = params.toString();
339329
@@ -359,31 +349,31 @@ router.post('/onering', jsonParser, async (request, response) => {
359});349});
360350
361router.post('/deeplx', jsonParser, async (request, response) => {351router.post('/deeplx', jsonParser, async (request, response) => {
362 const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL);352 try {
363 const url = secretUrl || DEEPLX_URL_DEFAULT;353 const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL);
354 const url = secretUrl || DEEPLX_URL_DEFAULT;
364355
365 if (!url) {356 if (!url) {
366 console.log('DeepLX URL is not configured.');357 console.log('DeepLX URL is not configured.');
367 return response.sendStatus(400);358 return response.sendStatus(400);
368 }359 }
369360
370 if (!secretUrl && url === DEEPLX_URL_DEFAULT) {361 if (!secretUrl && url === DEEPLX_URL_DEFAULT) {
371 console.log('DeepLX URL is using default value.', DEEPLX_URL_DEFAULT);362 console.log('DeepLX URL is using default value.', DEEPLX_URL_DEFAULT);
372 }363 }
373364
374 const text = request.body.text;365 const text = request.body.text;
375 let lang = request.body.lang;366 let lang = request.body.lang;
376 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {367 if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
377 lang = 'ZH';368 lang = 'ZH';
378 }369 }
379370
380 if (!text || !lang) {371 if (!text || !lang) {
381 return response.sendStatus(400);372 return response.sendStatus(400);
382 }373 }
383374
384 console.log('Input text: ' + text);375 console.log('Input text: ' + text);
385376
386 try {
387 const result = await fetch(url, {377 const result = await fetch(url, {
388 method: 'POST',378 method: 'POST',
389 body: JSON.stringify({379 body: JSON.stringify({