Clean-up try/catch blocks in translate.js
| @@ -41,6 +41,7 @@ function decodeBuffer(buffer) { | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | router.post('/libre', jsonParser, async (request, response) => { | 43 | router.post('/libre', jsonParser, async (request, response) => { |
| 44 | try { | ||
| 44 | const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE); | 45 | const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE); |
| 45 | const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL); | 46 | const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL); |
| 46 | 47 | ||
| @@ -70,7 +71,6 @@ router.post('/libre', jsonParser, async (request, response) => { | |||
| 70 | 71 | ||
| 71 | console.log('Input text: ' + text); | 72 | console.log('Input text: ' + text); |
| 72 | 73 | ||
| 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 | } |
| 200 | 200 | ||
| 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,7 +215,6 @@ router.post('/lingva', jsonParser, async (request, response) => { | |||
| 220 | 215 | ||
| 221 | console.log('Input text: ' + text); | 216 | console.log('Input text: ' + text); |
| 222 | 217 | ||
| 223 | try { | ||
| 224 | const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text)); | 218 | const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text)); |
| 225 | const result = await fetch(url); | 219 | const result = await fetch(url); |
| 226 | 220 | ||
| @@ -234,16 +228,13 @@ router.post('/lingva', jsonParser, async (request, response) => { | |||
| 234 | console.log('Translated text: ' + data.translation); | 228 | console.log('Translated text: ' + data.translation); |
| 235 | return response.send(data.translation); | 229 | return response.send(data.translation); |
| 236 | } catch (error) { | 230 | } catch (error) { |
| 237 | console.log('Translation error:', error); | ||
| 238 | return response.sendStatus(500); | ||
| 239 | } | ||
| 240 | } catch (error) { | ||
| 241 | console.log('Translation error', error); | 231 | console.log('Translation error', error); |
| 242 | return response.sendStatus(500); | 232 | return response.sendStatus(500); |
| 243 | } | 233 | } |
| 244 | }); | 234 | }); |
| 245 | 235 | ||
| 246 | router.post('/deepl', jsonParser, async (request, response) => { | 236 | router.post('/deepl', jsonParser, async (request, response) => { |
| 237 | try { | ||
| 247 | const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL); | 238 | const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL); |
| 248 | 239 | ||
| 249 | if (!key) { | 240 | if (!key) { |
| @@ -273,7 +264,6 @@ router.post('/deepl', jsonParser, async (request, response) => { | |||
| 273 | params.append('formality', formality); | 264 | params.append('formality', formality); |
| 274 | } | 265 | } |
| 275 | 266 | ||
| 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,6 +292,7 @@ router.post('/deepl', jsonParser, async (request, response) => { | |||
| 302 | }); | 292 | }); |
| 303 | 293 | ||
| 304 | router.post('/onering', jsonParser, async (request, response) => { | 294 | router.post('/onering', jsonParser, async (request, response) => { |
| 295 | try { | ||
| 305 | const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL); | 296 | const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL); |
| 306 | const url = secretUrl || ONERING_URL_DEFAULT; | 297 | const url = secretUrl || ONERING_URL_DEFAULT; |
| 307 | 298 | ||
| @@ -333,7 +324,6 @@ router.post('/onering', jsonParser, async (request, response) => { | |||
| 333 | 324 | ||
| 334 | console.log('Input text: ' + text); | 325 | console.log('Input text: ' + text); |
| 335 | 326 | ||
| 336 | try { | ||
| 337 | const fetchUrl = new URL(url); | 327 | const fetchUrl = new URL(url); |
| 338 | fetchUrl.search = params.toString(); | 328 | fetchUrl.search = params.toString(); |
| 339 | 329 | ||
| @@ -359,6 +349,7 @@ router.post('/onering', jsonParser, async (request, response) => { | |||
| 359 | }); | 349 | }); |
| 360 | 350 | ||
| 361 | router.post('/deeplx', jsonParser, async (request, response) => { | 351 | router.post('/deeplx', jsonParser, async (request, response) => { |
| 352 | try { | ||
| 362 | const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL); | 353 | const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL); |
| 363 | const url = secretUrl || DEEPLX_URL_DEFAULT; | 354 | const url = secretUrl || DEEPLX_URL_DEFAULT; |
| 364 | 355 | ||
| @@ -383,7 +374,6 @@ router.post('/deeplx', jsonParser, async (request, response) => { | |||
| 383 | 374 | ||
| 384 | console.log('Input text: ' + text); | 375 | console.log('Input text: ' + text); |
| 385 | 376 | ||
| 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({ |