mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Don't crash server if google translate fails
This commit is contained in:
@@ -58,6 +58,7 @@ function registerEndpoints(app, jsonParser) {
|
||||
});
|
||||
|
||||
app.post('/api/translate/google', jsonParser, async (request, response) => {
|
||||
try {
|
||||
const { generateRequestUrl, normaliseResponse } = require('google-translate-api-browser');
|
||||
const text = request.body.text;
|
||||
const lang = request.body.lang;
|
||||
@@ -78,14 +79,23 @@ function registerEndpoints(app, jsonParser) {
|
||||
});
|
||||
|
||||
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);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log("Translation error", error);
|
||||
return response.sendStatus(500);
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/translate/deepl', jsonParser, async (request, response) => {
|
||||
|
Reference in New Issue
Block a user