diff options
author | Andrew Dolgov <[email protected]> | 2018-12-30 15:32:42 +0300 |
---|---|---|
committer | Andrew Dolgov <[email protected]> | 2018-12-30 15:32:42 +0300 |
commit | fdb1698b9d514ea7a6b006a03ad70ba74084bb3c (patch) | |
tree | cf2e9504c81c1b6bfc50c51a5936867a202f9a78 /js/read.js | |
parent | f83c0f4425dc150c4b4c3a3b1140d83825946402 (diff) |
better error handling when downloading books
Diffstat (limited to 'js/read.js')
-rw-r--r-- | js/read.js | 64 |
1 files changed, 38 insertions, 26 deletions
@@ -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.<br/><small>" + e + "</small>"); + }); }; + fileReader.onerror = function(e) { + console.log('filereader error', e); + $(".loading_message").html("Unable to open book.<br/><small>" + e + "</small>"); + } + fileReader.readAsArrayBuffer(blob); + }).catch((e) => { + console.log('blob error', e); + $(".loading_message").html("Unable to download book.<br/><small>" + e + "</small>"); }); } 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).<br/><small>" + e + "</small>"); } }); |