From 67f27986727c6c1355e90e6b792c9d77cc1916c2 Mon Sep 17 00:00:00 2001 From: Ajay Ramachandran Date: Tue, 26 Jan 2021 21:57:43 -0500 Subject: Add error message from server when voting --- src/background.ts | 9 ++++++--- src/content.ts | 4 ++-- src/popup.ts | 8 ++++---- src/utils.ts | 5 +++-- 4 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/background.ts b/src/background.ts index ea011576..b562c4e7 100644 --- a/src/background.ts +++ b/src/background.ts @@ -125,19 +125,22 @@ async function submitVote(type: number, UUID: string, category: string) { if (response.ok) { return { - successType: 1 + successType: 1, + responseText: await response.text() }; } else if (response.status == 405) { //duplicate vote return { successType: 0, - statusCode: response.status + statusCode: response.status, + responseText: await response.text() }; } else { //error while connect return { successType: -1, - statusCode: response.status + statusCode: response.status, + responseText: await response.text() }; } } diff --git a/src/content.ts b/src/content.ts index 048d8898..1eaea00e 100644 --- a/src/content.ts +++ b/src/content.ts @@ -1373,7 +1373,7 @@ function vote(type: number, UUID: string, category?: string, skipNotice?: SkipNo //success (treat rate limits as a success) skipNotice.afterVote.bind(skipNotice)(utils.getSponsorTimeFromUUID(sponsorTimes, UUID), type, category); } else if (response.successType == -1) { - skipNotice.setNoticeInfoMessage.bind(skipNotice)(utils.getErrorMessage(response.statusCode)) + skipNotice.setNoticeInfoMessage.bind(skipNotice)(utils.getErrorMessage(response.statusCode, response.responseText)) skipNotice.resetVoteButtonInfo.bind(skipNotice)(); } } @@ -1500,7 +1500,7 @@ async function sendSubmitMessage(): Promise { document.getElementById("submitButton").style.animation = "unset"; ( document.getElementById("submitImage")).src = chrome.extension.getURL("icons/PlayerUploadFailedIconSponsorBlocker256px.png"); - alert(utils.getErrorMessage(response.status) + "\n\n" + (response.responseText)); + alert(utils.getErrorMessage(response.status, response.responseText)); } } diff --git a/src/popup.ts b/src/popup.ts index db34325c..f28589a7 100644 --- a/src/popup.ts +++ b/src/popup.ts @@ -548,7 +548,7 @@ async function runThePopup(messageListener?: MessageListener): Promise { PageElements.sponsorTimesContributionsContainer.classList.remove("hidden"); } else { - PageElements.setUsernameStatus.innerText = utils.getErrorMessage(response.status); + PageElements.setUsernameStatus.innerText = utils.getErrorMessage(response.status, response.responseText); } }); @@ -575,7 +575,7 @@ async function runThePopup(messageListener?: MessageListener): Promise { function vote(type, UUID) { //add loading info - addVoteMessage(chrome.i18n.getMessage("Loading"), UUID) + addVoteMessage(chrome.i18n.getMessage("Loading"), UUID); //send the vote message to the tab chrome.runtime.sendMessage({ @@ -587,9 +587,9 @@ async function runThePopup(messageListener?: MessageListener): Promise { //see if it was a success or failure if (response.successType == 1 || (response.successType == -1 && response.statusCode == 429)) { //success (treat rate limits as a success) - addVoteMessage(chrome.i18n.getMessage("voted"), UUID) + addVoteMessage(chrome.i18n.getMessage("voted"), UUID); } else if (response.successType == -1) { - addVoteMessage(utils.getErrorMessage(response.statusCode), UUID) + addVoteMessage(utils.getErrorMessage(response.statusCode, response.responseText), UUID); } } }); diff --git a/src/utils.ts b/src/utils.ts index 6d7af44d..fd47b27c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -286,8 +286,9 @@ class Utils { * @param {int} statusCode * @returns {string} errorMessage */ - getErrorMessage(statusCode: number): string { + getErrorMessage(statusCode: number, responseText: string): string { let errorMessage = ""; + const postFix = (responseText ? "\n\n" + responseText : ""); if([400, 429, 409, 502, 0].includes(statusCode)) { //treat them the same @@ -299,7 +300,7 @@ class Utils { errorMessage = chrome.i18n.getMessage("connectionError") + statusCode; } - return errorMessage; + return errorMessage + postFix; } /** -- cgit v1.2.3