From 9826c6eb1ecd501ac3eb1e9d9a98a3e6edde3fd0 Mon Sep 17 00:00:00 2001
From: Jakob Ackermann <jakob.ackermann@overleaf.com>
Date: Mon, 5 Jul 2021 16:23:18 +0100
Subject: [PATCH] [misc] fix handling of fetch errors (backport)

Testing:
- delete the pdf file while the initial request is inflight
- delete the pdf file after the initial request has finished
---
 src/core/chunked_stream.js  | 8 +++-----
 src/display/fetch_stream.js | 2 +-
 2 files changed, 4 insertions(+), 6 deletions(-)

diff --git a/src/core/chunked_stream.js b/src/core/chunked_stream.js
index 26a93f673..ab39037c1 100644
--- a/src/core/chunked_stream.js
+++ b/src/core/chunked_stream.js
@@ -307,7 +307,7 @@ class ChunkedStreamManager {
     }
 
     let chunks = [], loaded = 0;
-    const promise = new Promise((resolve, reject) => {
+    return new Promise((resolve, reject) => {
       const readChunk = (chunk) => {
         try {
           if (!chunk.done) {
@@ -328,14 +328,12 @@ class ChunkedStreamManager {
         }
       };
       rangeReader.read().then(readChunk, reject);
-    });
-    promise.then((data) => {
+    }).then((data) => {
       if (this.aborted) {
         return; // Ignoring any data after abort.
       }
       this.onReceiveData({ chunk: data, begin, });
     });
-    // TODO check errors
   }
 
   /**
@@ -384,7 +382,7 @@ class ChunkedStreamManager {
     for (const groupedChunk of groupedChunksToRequest) {
       const begin = groupedChunk.beginChunk * this.chunkSize;
       const end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
-      this.sendRequest(begin, end);
+      this.sendRequest(begin, end).catch(capability.reject);
     }
 
     return capability.promise;
diff --git a/src/display/fetch_stream.js b/src/display/fetch_stream.js
index f70ed1d54..e736c9494 100644
--- a/src/display/fetch_stream.js
+++ b/src/display/fetch_stream.js
@@ -222,7 +222,7 @@ class PDFFetchStreamRangeReader {
       }
       this._readCapability.resolve();
       this._reader = response.body.getReader();
-    });
+    }).catch(this._readCapability.reject);
 
     this.onProgress = null;
   }
-- 
2.17.1

