Clean-up try/catch blocks in translate.js
| @@ -41,36 +41,36 @@ function decodeBuffer(buffer) { | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | router.post('/libre', jsonParser, async (request, response) => { | 43 | router.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); | ||
| 46 | 47 | ||
| 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 | } |
| 51 | 52 | ||
| 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 | } |
| 55 | 56 | ||
| 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 | } |
| 59 | 60 | ||
| 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 | } |
| 63 | 64 | ||
| 64 | const text = request.body.text; | 65 | const text = request.body.text; |
| 65 | const lang = request.body.lang; | 66 | const lang = request.body.lang; |
| 66 | 67 | ||
| 67 | if (!text || !lang) { | 68 | if (!text || !lang) { |
| 68 | return response.sendStatus(400); | 69 | return response.sendStatus(400); |
| 69 | } | 70 | } |
| 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,23 +215,18 @@ 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 { | 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 | |
| 226 | 221 | 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 | }); |
| 245 | 235 | ||
| 246 | router.post('/deepl', jsonParser, async (request, response) => { | 236 | router.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); | ||
| 248 | 239 | ||
| 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 | } |
| 253 | 244 | ||
| 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 | } |
| 257 | 248 | ||
| 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'); |
| 261 | 252 | ||
| 262 | if (!text || !lang) { | 253 | if (!text || !lang) { |
| 263 | return response.sendStatus(400); | 254 | return response.sendStatus(400); |
| 264 | } | 255 | } |
| 265 | 256 | ||
| 266 | console.log('Input text: ' + text); | 257 | console.log('Input text: ' + text); |
| 267 | 258 | ||
| 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); |
| 271 | 262 | ||
| 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 | } |
| 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,38 +292,38 @@ 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) => { |
| 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; | ||
| 307 | 298 | ||
| 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 | } |
| 312 | 303 | ||
| 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 | } |
| 316 | 307 | ||
| 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 | } |
| 320 | 311 | ||
| 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; |
| 324 | 315 | ||
| 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 | } |
| 328 | 319 | ||
| 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); |
| 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,31 +349,31 @@ 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) => { |
| 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; | ||
| 364 | 355 | ||
| 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 | } |
| 369 | 360 | ||
| 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 | } |
| 373 | 364 | ||
| 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 | } |
| 379 | 370 | ||
| 380 | if (!text || !lang) { | 371 | if (!text || !lang) { |
| 381 | return response.sendStatus(400); | 372 | return response.sendStatus(400); |
| 382 | } | 373 | } |
| 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({ |