diff options
author | Ajay Ramachandran <[email protected]> | 2019-12-13 21:32:57 -0500 |
---|---|---|
committer | Ajay Ramachandran <[email protected]> | 2019-12-13 21:32:57 -0500 |
commit | b34e6076e40793fd56a847b551e4fb6d9c508f2e (patch) | |
tree | 4cb1b31bcedc10fcc9bfc90fcd633032062b63b0 | |
parent | 226c5d601c7ccc4e81ec8ecc2a30ea7e15367a0e (diff) | |
download | SponsorBlock-b34e6076e40793fd56a847b551e4fb6d9c508f2e.tar.gz SponsorBlock-b34e6076e40793fd56a847b551e4fb6d9c508f2e.zip |
Moved error message handling to a util file and added a link to the status site.
-rw-r--r-- | _locales/en/messages.json | 3 | ||||
-rw-r--r-- | content.js | 19 | ||||
-rw-r--r-- | popup.js | 32 | ||||
-rw-r--r-- | utils.js | 21 |
4 files changed, 31 insertions, 44 deletions
diff --git a/_locales/en/messages.json b/_locales/en/messages.json index 6d2b40dc..c0cb80bf 100644 --- a/_locales/en/messages.json +++ b/_locales/en/messages.json @@ -317,5 +317,8 @@ }, "guildlinesSummary": { "message": "- Make sure your segment only contains paid promotion segment, nothing else.\n- Make sure skipping this segment will not skip valuable content\n- If the whole video is a sponsor, please do not report it. A full video reporting system will come out soon.\n- Please do not report disclaimers that could show bias (if a review video is sponsored, don't skip when they mention that)." + }, + "statusReminder": { + "message": "Check status.sponsor.ajay.app for server status." } } @@ -980,14 +980,8 @@ function vote(type, UUID, skipNotice) { skipNotice.addNoticeInfoMessage.bind(skipNotice)(chrome.i18n.getMessage("voteFail")) skipNotice.resetVoteButtonInfo.bind(skipNotice)(); } else if (response.successType == -1) { - if (response.statusCode == 502) { - skipNotice.addNoticeInfoMessage.bind(skipNotice)(chrome.i18n.getMessage("serverDown")) - skipNotice.resetVoteButtonInfo.bind(skipNotice)(); - } else { - //failure: unknown error - skipNotice.addNoticeInfoMessage.bind(skipNotice)(chrome.i18n.getMessage("connectionError") + response.statusCode); - skipNotice.resetVoteButtonInfo.bind(skipNotice)(); - } + skipNotice.addNoticeInfoMessage.bind(skipNotice)(getErrorMessage(response.statusCode)) + skipNotice.resetVoteButtonInfo.bind(skipNotice)(); } } } @@ -1101,14 +1095,7 @@ function sendSubmitMessage(){ document.getElementById("submitButton").style.animation = "unset"; document.getElementById("submitImage").src = chrome.extension.getURL("icons/PlayerUploadFailedIconSponsorBlocker256px.png"); - if([400, 429, 409, 502, 0].includes(response.statusCode)) { - //treat them the same - if (response.statusCode == 503) response.statusCode = 502; - - alert(chrome.i18n.getMessage(response.statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + response.statusCode); - } else { - alert(chrome.i18n.getMessage("connectionError") + response.statusCode); - } + alert(getErrorMessage(response.statusCode)); } } }); @@ -895,18 +895,7 @@ function runThePopup() { clearTimes(); } else { - let errorMessage = ""; - - if([400, 429, 409, 502, 0].includes(response.statusCode)) { - //treat them the same - if (response.statusCode == 503) response.statusCode = 502; - - errorMessage = chrome.i18n.getMessage(response.statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + response.statusCode; - } else { - errorMessage = chrome.i18n.getMessage("connectionError") + response.statusCode; - } - - document.getElementById("submitTimesInfoMessage").innerText = errorMessage; + document.getElementById("submitTimesInfoMessage").innerText = getErrorMessage(response.statusCode); document.getElementById("submitTimesInfoMessageContainer").style.display = "unset"; SB.submitTimesInfoMessageContainer.style.display = "unset"; @@ -1138,7 +1127,7 @@ function runThePopup() { SB.usernameInput.style.display = "none"; SB.setUsernameStatusContainer.style.display = "unset"; - SB.setUsernameStatus.innerText = "Couldn't connect to server. Error code: " + xmlhttp.status; + SB.setUsernameStatus.innerText = getErrorMessage(xmlhttp.status); } }); }); @@ -1160,15 +1149,7 @@ function runThePopup() { SB.setUsernameStatus.innerText = chrome.i18n.getMessage("success"); } else if (xmlhttp.readyState == 4) { - let errorMessage = ""; - - if([400, 429, 409, 502].includes(xmlhttp.status)) { - errorMessage = chrome.i18n.getMessage(xmlhttp.status); - } else { - errorMessage = chrome.i18n.getMessage("connectionError") + xmlhttp.status; - } - - SB.setUsernameStatus.innerText = errorMessage; + SB.setUsernameStatus.innerText = getErrorMessageI(xmlhttp.status); } }); }); @@ -1224,12 +1205,7 @@ function runThePopup() { //failure: duplicate vote addVoteMessage(chrome.i18n.getMessage("voteFail"), UUID) } else if (response.successType == -1) { - if (response.statusCode == 502) { - addVoteMessage(chrome.i18n.getMessage("serverDown"), UUID) - } else { - //failure: unknown error - addVoteMessage(chrome.i18n.getMessage("connectionError") + response.statusCode, UUID) - } + addVoteMessage(getErrorMessage(response.statusCode), UUID) } } }); @@ -66,3 +66,24 @@ function localizeHtmlPage() { } } +/** + * Gets the error message in a nice string + * + * @param {int} statusCode + * @returns {string} errorMessage + */ +function getErrorMessage(statusCode) { + let errorMessage = ""; + + if([400, 429, 409, 502, 0].includes(statusCode)) { + //treat them the same + if (statusCode == 503) statusCode = 502; + + errorMessage = chrome.i18n.getMessage(statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + statusCode + + "\n\n" + chrome.i18n.getMessage("statusReminder"); + } else { + errorMessage = chrome.i18n.getMessage("connectionError") + statusCode; + } + + return errorMessage; +}
\ No newline at end of file |