From fdb1698b9d514ea7a6b006a03ad70ba74084bb3c Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Sun, 30 Dec 2018 15:32:42 +0300 Subject: better error handling when downloading books --- js/read.js | 64 +++++++++++++++++++++++++++++++++++++------------------------- 1 file changed, 38 insertions(+), 26 deletions(-) (limited to 'js') diff --git a/js/read.js b/js/read.js index 04d2c15..5430b1a 100644 --- a/js/read.js +++ b/js/read.js @@ -118,47 +118,59 @@ function init_reader() { if (resp.status == 200) { const bookId = $.urlParam("b"); - // let's store this for later - localforage.setItem(cacheId('book'), resp.clone().blob()); - - // if there's no base information cached yet, let's do that too - localforage.getItem(cacheId()).then(function(info) { - if (!info) { - $.post("backend.php", {op: "getinfo", id: bookId }, function(data) { - if (data) { - localforage.setItem(cacheId(), data); - - if (data.has_cover) { - fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { - if (resp.status == 200) { - localforage.setItem(cacheId('cover'), resp.blob()); - } - }); - } - } - }); - } - }); - resp.blob().then(function(blob) { + // if there's no base information cached yet, let's do that too + localforage.getItem(cacheId()).then(function(info) { + if (!info) { + $.post("backend.php", {op: "getinfo", id: bookId }, function(data) { + if (data) { + localforage.setItem(cacheId(), data); + + if (data.has_cover) { + fetch("backend.php?op=cover&id=" + bookId, {credentials: 'same-origin'}).then(function(resp) { + if (resp.status == 200) { + localforage.setItem(cacheId('cover'), resp.blob()); + } + }); + } + } + }); + } + }); + const fileReader = new FileReader(); fileReader.onload = function() { - book.open(this.result); + book.open(this.result).then(() => { + + // let's store this for later + localforage.setItem(cacheId('book'), blob); + + }).catch((e) => { + $(".loading_message").html("Unable to open book.
" + e + ""); + }); }; + fileReader.onerror = function(e) { + console.log('filereader error', e); + $(".loading_message").html("Unable to open book.
" + e + ""); + } + fileReader.readAsArrayBuffer(blob); + }).catch((e) => { + console.log('blob error', e); + $(".loading_message").html("Unable to download book.
" + e + ""); }); } else { $(".loading_message").html("Unable to download book: " + resp.status + "."); } - }).catch(function(err) { - console.warn(err); + }).catch(function(e) { + console.warn(e); if ($(".loading").is(":visible")) { - $(".loading_message").html("Unable to load book (remote)."); + $(".loading_message").html("Unable to load book (remote).
" + e + ""); } }); -- cgit v1.2.3