Skip to content

Commit

Permalink
Merge pull request #146 from TurboWarp/translate-fail-handling
Browse files Browse the repository at this point in the history
Return original message if translation request fails
  • Loading branch information
GarboMuffin authored Jul 8, 2023
2 parents 7b32cfc + a515991 commit 9ee3974
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/extensions/scratch3_translate/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ class Scratch3TranslateBlocks {
})
.catch(err => {
log.warn(`error fetching translate result! ${err}`);
return '';
return args.WORDS;
});
return translatePromise;
}
Expand Down
3 changes: 3 additions & 0 deletions src/util/fetch-with-timeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ const fetchWithTimeout = (resource, init, timeout) => {
fetch(resource, Object.assign({signal}, init)).then(response => {
clearTimeout(timeoutID);
return response;
}, error => {
clearTimeout(timeoutID);
throw error;
}),
new Promise((resolve, reject) => {
timeoutID = setTimeout(() => {
Expand Down
23 changes: 23 additions & 0 deletions test/unit/tw_translate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const {test} = require('tap');
const Scratch3TranslateBlocks = require('../../src/extensions/scratch3_translate/index');

global.navigator = {
language: 'en'
};

// Translate tries to access AbortController from window, but does not require it to exist.
global.window = {};

test('translate returns original string on network error', t => {
t.plan(1);

// Simulate the network being down or filtered
global.fetch = () => Promise.reject(new Error('Simulated network error'));

const extension = new Scratch3TranslateBlocks();
extension.getTranslate({WORDS: 'My message 123123', LANGUAGE: 'es'})
.then(message => {
t.equal(message, 'My message 123123');
t.end();
});
});

0 comments on commit 9ee3974

Please sign in to comment.