Skip to content

Commit

Permalink
User can retry HTTP calls.
Browse files Browse the repository at this point in the history
Refer: #128
  • Loading branch information
dteviot committed Sep 3, 2017
1 parent 723006f commit af67be2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 10 deletions.
8 changes: 8 additions & 0 deletions plugin/_locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@
"message": "Edit Chapter URLs",
"description": "Label on button to allow manually editing list of URLs to fetch"
},
"__MSG_button_error_Cancel__": {
"message": "Cancel",
"description": "Label on 'Cancel' button when show error"
},
"__MSG_button_error_OK__": {
"message": "OK",
"description": "Label on 'OK' button when show error"
},
"__MSG_button_error_Retry__": {
"message": "Retry",
"description": "Label on 'Retry' button when show error"
},
"__MSG_button_Fetch_Chapters__": {
"message": "Fetch Chapters",
"description": "Label on 'Fetch Chapters' button under 'Developer Stuff'"
Expand Down
43 changes: 36 additions & 7 deletions plugin/js/ErrorLog.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ class ErrorLog {
ErrorLog.getErrorSection().hidden = false;

ErrorLog.setErrorMessageText(msg);
document.getElementById("errorButtonOk").onclick = function () {
ErrorLog.queue.shift();
if (ErrorLog.queue.length === 0) {
ErrorLog.restoreSectionVisibility(sections);
} else {
ErrorLog.setErrorMessageText(ErrorLog.queue[0]);
};
ErrorLog.setErrorMessageButtons(msg, sections);
}

static onCloseError(sections) {
ErrorLog.queue.shift();
if (ErrorLog.queue.length === 0) {
ErrorLog.restoreSectionVisibility(sections);
} else {
ErrorLog.setErrorMessageText(ErrorLog.queue[0]);
ErrorLog.setErrorMessageButtons(ErrorLog.queue[0], sections);
};
}

Expand Down Expand Up @@ -63,6 +66,32 @@ class ErrorLog {
ErrorLog.history.push(textRow.textContent);
}

/** private */
static setErrorMessageButtons(msg, sections) {
let close = () => ErrorLog.onCloseError(sections);
let okButton = document.getElementById("errorButtonOk");
let retryButton = document.getElementById("errorButtonRetry");
let cancelButton = document.getElementById("errorButtonCancel");
if (msg.retryAction !== undefined) {
okButton.hidden = true;
retryButton.hidden = false;
retryButton.onclick = function() {
close();
msg.retryAction();
};
cancelButton.hidden = false;
cancelButton.onclick = function() {
close();
msg.cancelAction();
};
} else {
okButton.hidden = false;
okButton.onclick = close;
retryButton.hidden = true;
cancelButton.hidden = true;
}
}

/** private */
static restoreSectionVisibility(sections) {
for(let [key,value] of sections) {
Expand Down
11 changes: 8 additions & 3 deletions plugin/js/HttpClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@ class HttpClient {

static wrapFetchImpl(url, handler) {
return fetch(url, HttpClient.makeOptions()).then(function(response) {
return HttpClient.checkResponseAndGetData(handler, response);
return HttpClient.checkResponseAndGetData(url, handler, response);
}).catch(function (error) {
let errorMsg = chrome.i18n.getMessage("htmlFetchFailed", [url, error.message]);
return Promise.reject(new Error(errorMsg));
});
}

static checkResponseAndGetData(handler, response) {
static checkResponseAndGetData(url, handler, response) {
if(!response.ok) {
let errorMsg = chrome.i18n.getMessage("htmlFetchFailed", [response.url, response.status]);
return Promise.reject(new Error(errorMsg));
let msg = new Error(errorMsg);
return new Promise(function(resolve, reject) {
msg.retryAction = () => resolve(HttpClient.wrapFetchImpl(url, handler));
msg.cancelAction = () => reject(new Error(errorMsg));
ErrorLog.showErrorMessage(msg);
});
} else {
handler.setResponse(response);
return handler.extractContentFromResponse(response);
Expand Down
2 changes: 2 additions & 0 deletions plugin/popup.html
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
<tr>
<td>
<button id="errorButtonOk">__MSG_button_error_OK__</button>
<button id="errorButtonRetry">__MSG_button_error_Retry__</button>
<button id="errorButtonCancel">__MSG_button_error_Cancel__</button>
</td>
</tr>
</table>
Expand Down

0 comments on commit af67be2

Please sign in to comment.