Improving error handling.

This commit is contained in:
Vahagn Khachatryan
2019-01-27 23:52:00 +00:00
parent e34a2a2cf6
commit 4d4102e8ee

View File

@@ -17,7 +17,7 @@ function getCurrentTab() {
console.error(`Expected 1 active tab, received: ${tabs}`); console.error(`Expected 1 active tab, received: ${tabs}`);
throw 'Failed to get active tab.'; throw 'Failed to get active tab.';
} }
}, onError); });
} }
function extractBookId(url){ function extractBookId(url){
@@ -52,28 +52,60 @@ class Book{
this.page = page this.page = page
} }
downloadResource(url){ downloadResource(url, retry=false){
console.info(`Downloading ${url}`) if (retry){
console.warn(`Retry ${url}`)
} else {
console.info(`Downloading ${url}`)
}
return fetch(url, { return fetch(url, {
credentials: 'include', credentials: 'include',
mode: "no-cors" // no-cors, cors, *same-origin mode: "no-cors" // no-cors, cors, *same-origin
}) })
.catch((error)=>{ .catch((error)=>{
console.info(`Failed ${url} - ${error.message}`) if (!retry){
console.info(`Retry ${url}`) console.warn(`Failed ${url} - ${error.message}`)
// If failed to download, try second time. return this.downloadResource(url, true)
return fetch(url, { } else {
credentials: 'include', console.error(`Giving up on ${url} - ${error.message}`)
mode: "no-cors" // no-cors, cors, *same-origin 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){ downloadJson(url){
return this.downloadResource(url) return this.downloadAndReport(url)
.then((res) => { .then((res) => {
return res.json(); return res.json();
}, onError); });
} }
downloadBookInfo(){ downloadBookInfo(){
@@ -82,7 +114,7 @@ class Book{
return this.downloadJson(url) return this.downloadJson(url)
.then((book_info) => { .then((book_info) => {
this.book_info = book_info; this.book_info = book_info;
}, onError); });
} }
downloadMetaContent(){ downloadMetaContent(){
@@ -104,21 +136,17 @@ class Book{
})) }))
downloads.push( downloads.push(
Promise.map(this.book_info.chapters, (chapter) => { Promise.map(this.book_info.chapters, (chapter) => {
let item_id = this.page.renderProgress(`${chapter}`)
return this.downloadJson(chapter) return this.downloadJson(chapter)
.then((json) => { .then((json) => {
this.chapter_info[chapter] = json this.chapter_info[chapter] = json
return this.extractChapterAssets(json) return this.extractChapterAssets(json)
}) })
.then(()=>{ }, {concurrency: concurrency}))
this.page.renderDone(item_id)
})
.catch((error)=>{
this.page.renderFail(item_id, error)
})
},{concurrency: concurrency}))
return Promise.all(downloads) return Promise.all(downloads)
.catch((error)=>{
throw `Failed to fetch meta data: "${error}"`
})
} }
@@ -146,8 +174,7 @@ class Book{
downloadContent(){ downloadContent(){
return Promise.map(Object.keys(this.book_files), (url) => { return Promise.map(Object.keys(this.book_files), (url) => {
let item_id = this.page.renderProgress(`${this.book_files[url].filename}`) return this.downloadAndReport(url, this.book_files[url].filename)
return this.downloadResource(url)
.then((res) => { .then((res) => {
if (res.ok){ if (res.ok){
this.book_files[url].headers = res.headers this.book_files[url].headers = res.headers
@@ -157,14 +184,10 @@ class Book{
}) })
} }
}) })
.then((arrBuffer)=>{
this.page.renderDone(item_id)
return arrBuffer
})
.catch((error)=>{ .catch((error)=>{
this.page.renderFail(item_id, error) throw `Failed to download book content: "${error}"`
}) })
},{concurrency: concurrency}) },{concurrency: concurrency})
} }
insertBookFile(url, filename){ insertBookFile(url, filename){
@@ -198,6 +221,14 @@ class SidebarPage{
$('#loading').hide(); $('#loading').hide();
} }
renderFailedTheBook(error) {
console.error(`Error: ${error}`);
$('#loading').hide();
$('#error-message').text(`Error: ${error}`);
$('#error-message').show();
// $('#book-info').hide();
}
renderChapterList(book){ renderChapterList(book){
// Add chapters to UI // Add chapters to UI
for (let chapter_idx in book.chapter_list) { 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) function fillMetadata(epub, book)
{ {
@@ -363,11 +385,11 @@ function onDownloadBookClicked(){
page = new SidebarPage() page = new SidebarPage()
getCurrentTab() getCurrentTab()
.then(extractBookId, onError) .then(extractBookId)
.then((book_id) => { .then((book_id) => {
epub = new EpubWriter(); epub = new EpubWriter();
book = new Book(book_id, page); book = new Book(book_id, page);
book.downloadBookInfo() return book.downloadBookInfo()
.then(() => { page.renderInfo(book); }) .then(() => { page.renderInfo(book); })
// .then(() => { return book.downloadChapterList(); }) // .then(() => { return book.downloadChapterList(); })
// .then(() => { return page.renderChapterList(book); }) // .then(() => { return page.renderChapterList(book); })
@@ -386,11 +408,13 @@ function onDownloadBookClicked(){
let url = window.URL.createObjectURL(file) let url = window.URL.createObjectURL(file)
return browser.downloads.download({ "filename" : filename, url : url}) return browser.downloads.download({ "filename" : filename, url : url})
}) })
.then(() =>{ })
page.renderDoneWithBook(); .then(() =>{
}) page.renderDoneWithBook();
})
}, onError); .catch((error)=>{
page.renderFailedTheBook(error);
});
} }
document.addEventListener('DOMContentLoaded', function() { document.addEventListener('DOMContentLoaded', function() {