From 4d4102e8eef825e634640e7e44933f0bb4b836fd Mon Sep 17 00:00:00 2001 From: Vahagn Khachatryan Date: Sun, 27 Jan 2019 23:52:00 +0000 Subject: [PATCH] Improving error handling. --- src/sidebar.js | 114 ++++++++++++++++++++++++++++++------------------- 1 file changed, 69 insertions(+), 45 deletions(-) diff --git a/src/sidebar.js b/src/sidebar.js index 84722d2..5d4cdf3 100644 --- a/src/sidebar.js +++ b/src/sidebar.js @@ -17,7 +17,7 @@ function getCurrentTab() { console.error(`Expected 1 active tab, received: ${tabs}`); throw 'Failed to get active tab.'; } - }, onError); + }); } function extractBookId(url){ @@ -52,28 +52,60 @@ class Book{ this.page = page } - downloadResource(url){ - console.info(`Downloading ${url}`) + downloadResource(url, retry=false){ + if (retry){ + console.warn(`Retry ${url}`) + } else { + console.info(`Downloading ${url}`) + } return fetch(url, { credentials: 'include', mode: "no-cors" // no-cors, cors, *same-origin }) .catch((error)=>{ - console.info(`Failed ${url} - ${error.message}`) - console.info(`Retry ${url}`) - // If failed to download, try second time. - return fetch(url, { - credentials: 'include', - mode: "no-cors" // no-cors, cors, *same-origin - }) + if (!retry){ + console.warn(`Failed ${url} - ${error.message}`) + return this.downloadResource(url, true) + } else { + console.error(`Giving up on ${url} - ${error.message}`) + throw `Failed second try on ${url} - ${error.message}` + } + }) + .then((res)=>{ + if (res.ok) { + return res + } else if (!retry){ + console.warn(`Failed ${url} - ${res.status} - ${res.statusText}`) + return this.downloadResource(url, true) + } else { + console.error(`Giving up on ${url} - ${res.status} - ${res.statusText}`) + throw `Failed second try on ${url} - ${res.status} - ${res.statusText}` + } + }) + } + + downloadAndReport(url, item_text=null){ + let item_id = this.page.renderProgress(item_text ? item_text : url) + return this.downloadResource(url) + .then((res)=>{ + if (item_id){ + this.page.renderDone(item_id) + } + return res + }) + .catch((error)=>{ + if (item_id){ + this.page.renderFail(item_id, error) + throw error + } }) } downloadJson(url){ - return this.downloadResource(url) + return this.downloadAndReport(url) .then((res) => { return res.json(); - }, onError); + }); } downloadBookInfo(){ @@ -82,7 +114,7 @@ class Book{ return this.downloadJson(url) .then((book_info) => { this.book_info = book_info; - }, onError); + }); } downloadMetaContent(){ @@ -104,21 +136,17 @@ class Book{ })) downloads.push( Promise.map(this.book_info.chapters, (chapter) => { - let item_id = this.page.renderProgress(`${chapter}`) return this.downloadJson(chapter) .then((json) => { this.chapter_info[chapter] = json return this.extractChapterAssets(json) }) - .then(()=>{ - this.page.renderDone(item_id) - }) - .catch((error)=>{ - this.page.renderFail(item_id, error) - }) - },{concurrency: concurrency})) + }, {concurrency: concurrency})) return Promise.all(downloads) + .catch((error)=>{ + throw `Failed to fetch meta data: "${error}"` + }) } @@ -146,8 +174,7 @@ class Book{ downloadContent(){ return Promise.map(Object.keys(this.book_files), (url) => { - let item_id = this.page.renderProgress(`${this.book_files[url].filename}`) - return this.downloadResource(url) + return this.downloadAndReport(url, this.book_files[url].filename) .then((res) => { if (res.ok){ this.book_files[url].headers = res.headers @@ -157,14 +184,10 @@ class Book{ }) } }) - .then((arrBuffer)=>{ - this.page.renderDone(item_id) - return arrBuffer - }) .catch((error)=>{ - this.page.renderFail(item_id, error) + throw `Failed to download book content: "${error}"` }) - },{concurrency: concurrency}) + },{concurrency: concurrency}) } insertBookFile(url, filename){ @@ -198,6 +221,14 @@ class SidebarPage{ $('#loading').hide(); } + renderFailedTheBook(error) { + console.error(`Error: ${error}`); + $('#loading').hide(); + $('#error-message').text(`Error: ${error}`); + $('#error-message').show(); + // $('#book-info').hide(); + } + renderChapterList(book){ // Add chapters to UI for (let chapter_idx in book.chapter_list) { @@ -237,15 +268,6 @@ class SidebarPage{ } } -function onError(error) { - console.error(`Error: ${error}`); - $('#error-message').text(`Error: ${error}`); - $('#error-message').show(); - $('#loading').hide(); - $('#book-info').hide(); -} - - function fillMetadata(epub, book) { @@ -363,11 +385,11 @@ function onDownloadBookClicked(){ page = new SidebarPage() getCurrentTab() - .then(extractBookId, onError) + .then(extractBookId) .then((book_id) => { epub = new EpubWriter(); book = new Book(book_id, page); - book.downloadBookInfo() + return book.downloadBookInfo() .then(() => { page.renderInfo(book); }) // .then(() => { return book.downloadChapterList(); }) // .then(() => { return page.renderChapterList(book); }) @@ -386,11 +408,13 @@ function onDownloadBookClicked(){ let url = window.URL.createObjectURL(file) return browser.downloads.download({ "filename" : filename, url : url}) }) - .then(() =>{ - page.renderDoneWithBook(); - }) - - }, onError); + }) + .then(() =>{ + page.renderDoneWithBook(); + }) + .catch((error)=>{ + page.renderFailedTheBook(error); + }); } document.addEventListener('DOMContentLoaded', function() {