mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-04-13 18:32:05 +02:00
Clean-up try/catch blocks in translate.js
This commit is contained in:
parent
7490357d9e
commit
09dd9762f7
@ -41,6 +41,7 @@ function decodeBuffer(buffer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.post('/libre', jsonParser, async (request, response) => {
|
router.post('/libre', jsonParser, async (request, response) => {
|
||||||
|
try {
|
||||||
const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
|
const key = readSecret(request.user.directories, SECRET_KEYS.LIBRE);
|
||||||
const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
|
const url = readSecret(request.user.directories, SECRET_KEYS.LIBRE_URL);
|
||||||
|
|
||||||
@ -70,7 +71,6 @@ router.post('/libre', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await fetch(url, {
|
const result = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
@ -198,11 +198,6 @@ router.post('/lingva', jsonParser, async (request, response) => {
|
|||||||
console.log('Lingva URL is using default value.', LINGVA_DEFAULT);
|
console.log('Lingva URL is using default value.', LINGVA_DEFAULT);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!baseUrl) {
|
|
||||||
console.log('Lingva URL is not configured.');
|
|
||||||
return response.sendStatus(400);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
|
if (request.body.lang === 'zh-CN' || request.body.lang === 'zh-TW') {
|
||||||
request.body.lang = 'zh';
|
request.body.lang = 'zh';
|
||||||
}
|
}
|
||||||
@ -220,7 +215,6 @@ router.post('/lingva', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
try {
|
|
||||||
const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text));
|
const url = urlJoin(baseUrl, 'auto', lang, encodeURIComponent(text));
|
||||||
const result = await fetch(url);
|
const result = await fetch(url);
|
||||||
|
|
||||||
@ -233,10 +227,6 @@ router.post('/lingva', jsonParser, async (request, response) => {
|
|||||||
const data = await result.json();
|
const data = await result.json();
|
||||||
console.log('Translated text: ' + data.translation);
|
console.log('Translated text: ' + data.translation);
|
||||||
return response.send(data.translation);
|
return response.send(data.translation);
|
||||||
} catch (error) {
|
|
||||||
console.log('Translation error:', error);
|
|
||||||
return response.sendStatus(500);
|
|
||||||
}
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log('Translation error', error);
|
console.log('Translation error', error);
|
||||||
return response.sendStatus(500);
|
return response.sendStatus(500);
|
||||||
@ -244,6 +234,7 @@ router.post('/lingva', jsonParser, async (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/deepl', jsonParser, async (request, response) => {
|
router.post('/deepl', jsonParser, async (request, response) => {
|
||||||
|
try {
|
||||||
const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL);
|
const key = readSecret(request.user.directories, SECRET_KEYS.DEEPL);
|
||||||
|
|
||||||
if (!key) {
|
if (!key) {
|
||||||
@ -273,7 +264,6 @@ router.post('/deepl', jsonParser, async (request, response) => {
|
|||||||
params.append('formality', formality);
|
params.append('formality', formality);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await fetch('https://api-free.deepl.com/v2/translate', {
|
const result = await fetch('https://api-free.deepl.com/v2/translate', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: params,
|
body: params,
|
||||||
@ -302,6 +292,7 @@ router.post('/deepl', jsonParser, async (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/onering', jsonParser, async (request, response) => {
|
router.post('/onering', jsonParser, async (request, response) => {
|
||||||
|
try {
|
||||||
const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL);
|
const secretUrl = readSecret(request.user.directories, SECRET_KEYS.ONERING_URL);
|
||||||
const url = secretUrl || ONERING_URL_DEFAULT;
|
const url = secretUrl || ONERING_URL_DEFAULT;
|
||||||
|
|
||||||
@ -333,7 +324,6 @@ router.post('/onering', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
try {
|
|
||||||
const fetchUrl = new URL(url);
|
const fetchUrl = new URL(url);
|
||||||
fetchUrl.search = params.toString();
|
fetchUrl.search = params.toString();
|
||||||
|
|
||||||
@ -359,6 +349,7 @@ router.post('/onering', jsonParser, async (request, response) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.post('/deeplx', jsonParser, async (request, response) => {
|
router.post('/deeplx', jsonParser, async (request, response) => {
|
||||||
|
try {
|
||||||
const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL);
|
const secretUrl = readSecret(request.user.directories, SECRET_KEYS.DEEPLX_URL);
|
||||||
const url = secretUrl || DEEPLX_URL_DEFAULT;
|
const url = secretUrl || DEEPLX_URL_DEFAULT;
|
||||||
|
|
||||||
@ -383,7 +374,6 @@ router.post('/deeplx', jsonParser, async (request, response) => {
|
|||||||
|
|
||||||
console.log('Input text: ' + text);
|
console.log('Input text: ' + text);
|
||||||
|
|
||||||
try {
|
|
||||||
const result = await fetch(url, {
|
const result = await fetch(url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: JSON.stringify({
|
body: JSON.stringify({
|
||||||
|
Loading…
x
Reference in New Issue
Block a user