Skip to content

Commit

Permalink
Merge pull request #52 from imserhii/fix-limit-exceeded-error-handling
Browse files Browse the repository at this point in the history
Fixed
  • Loading branch information
gingerbeardman authored Jun 28, 2019
2 parents 41e58ad + d77d551 commit 1d9b78e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 14 deletions.
28 changes: 15 additions & 13 deletions Polyglot.safariextension/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ export async function translate(text, targetLanguage) {
})
const endpoint = `http://translate.googleapis.com/translate_a/single${query}`

try {
const response = await fetch(endpoint)
if (response.status === 503) {
throw new Error(REQUEST_LIMIT_EXCEEDED)
}
const response = await fetch(endpoint)

const body = await response.text()
const data = JSON.parse(
body.replace(/,,/g, ',null,').replace(/,,/g, ',null,')
)
const translatedText = data[0].map(sentence => sentence[0]).join('<br/>')
return translatedText
} catch (err) {
throw new Error(err)
if (response.status === 503) {
throw new Error(REQUEST_LIMIT_EXCEEDED)
}

const body = await response.text()

if (body.includes('86640')) {
throw new Error(REQUEST_LIMIT_EXCEEDED)
}

const data = JSON.parse(
body.replace(/,,/g, ',null,').replace(/,,/g, ',null,')
)
const translatedText = data[0].map(sentence => sentence[0]).join('<br/>')
return translatedText
}
2 changes: 1 addition & 1 deletion Polyglot.safariextension/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async function handleFinishedGetSelectedText(msg) {
const translatedText = await translate(message, targetLanguage)
target.page.dispatchMessage('updatePanel', translatedText)
} catch (err) {
target.page.dispatchMessage('updatePanel', err)
target.page.dispatchMessage('updatePanel', err.message)
}
}

Expand Down

0 comments on commit 1d9b78e

Please sign in to comment.