Don't crash server if google translate fails

This commit is contained in:
Cohee 2023-11-23 00:59:34 +02:00
parent 35349dd8d7
commit 45b714fb9e
1 changed files with 31 additions and 21 deletions

View File

@ -58,34 +58,44 @@ function registerEndpoints(app, jsonParser) {
});
app.post('/api/translate/google', jsonParser, async (request, response) => {
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
const text = request.body.text;
const lang = request.body.lang;
try {
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
const text = request.body.text;
const lang = request.body.lang;
if (!text || !lang) {
return response.sendStatus(400);
}
if (!text || !lang) {
return response.sendStatus(400);
}
console.log('Input text: ' + text);
console.log('Input text: ' + text);
const url = generateRequestUrl(text, { to: lang });
const url = generateRequestUrl(text, { to: lang });
https.get(url, (resp) => {
let data = '';
https.get(url, (resp) => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end', () => {
try {
const result = normaliseResponse(JSON.parse(data));
console.log('Translated text: ' + result.text);
return response.send(result.text);
} catch (error) {
console.log("Translation error", error);
return response.sendStatus(500);
}
});
}).on("error", (err) => {
console.log("Translation error: " + err.message);
return response.sendStatus(500);
});
resp.on('end', () => {
const result = normaliseResponse(JSON.parse(data));
console.log('Translated text: ' + result.text);
return response.send(result.text);
});
}).on("error", (err) => {
console.log("Translation error: " + err.message);
} catch (error) {
console.log("Translation error", error);
return response.sendStatus(500);
});
}
});
app.post('/api/translate/deepl', jsonParser, async (request, response) => {