From 4967c8e6c50fdd9eca6ebcc587a8fb80f4a169b3 Mon Sep 17 00:00:00 2001
From: Aiqiao Yan <aiqiaoy@MININT-K6RVDNE.europe.corp.microsoft.com>
Date: Fri, 8 May 2020 14:27:52 -0400
Subject: [PATCH] error handling for stream

---
 dist/restore/index.js  |  6 +++++-
 dist/save/index.js     |  6 +++++-
 src/cacheHttpClient.ts | 18 ++++++++++++------
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/dist/restore/index.js b/dist/restore/index.js
index d090f78..59a1b57 100644
--- a/dist/restore/index.js
+++ b/dist/restore/index.js
@@ -2379,11 +2379,15 @@ function uploadFile(httpClient, cacheId, archivePath) {
                     const start = offset;
                     const end = offset + chunkSize - 1;
                     offset += MAX_CHUNK_SIZE;
-                    yield uploadChunk(httpClient, resourceUrl, () => fs.createReadStream(archivePath, {
+                    yield uploadChunk(httpClient, resourceUrl, () => fs
+                        .createReadStream(archivePath, {
                         fd,
                         start,
                         end,
                         autoClose: false
+                    })
+                        .on("error", error => {
+                        throw new Error(`Cache upload failed because file read failed with ${error.Message}`);
                     }), start, end);
                 }
             })));
diff --git a/dist/save/index.js b/dist/save/index.js
index 51ee3e4..de9e2a5 100644
--- a/dist/save/index.js
+++ b/dist/save/index.js
@@ -2379,11 +2379,15 @@ function uploadFile(httpClient, cacheId, archivePath) {
                     const start = offset;
                     const end = offset + chunkSize - 1;
                     offset += MAX_CHUNK_SIZE;
-                    yield uploadChunk(httpClient, resourceUrl, () => fs.createReadStream(archivePath, {
+                    yield uploadChunk(httpClient, resourceUrl, () => fs
+                        .createReadStream(archivePath, {
                         fd,
                         start,
                         end,
                         autoClose: false
+                    })
+                        .on("error", error => {
+                        throw new Error(`Cache upload failed because file read failed with ${error.Message}`);
                     }), start, end);
                 }
             })));
diff --git a/src/cacheHttpClient.ts b/src/cacheHttpClient.ts
index 78f1251..9274d5b 100644
--- a/src/cacheHttpClient.ts
+++ b/src/cacheHttpClient.ts
@@ -295,12 +295,18 @@ async function uploadFile(
                         httpClient,
                         resourceUrl,
                         () =>
-                            fs.createReadStream(archivePath, {
-                                fd,
-                                start,
-                                end,
-                                autoClose: false
-                            }),
+                            fs
+                                .createReadStream(archivePath, {
+                                    fd,
+                                    start,
+                                    end,
+                                    autoClose: false
+                                })
+                                .on("error", error => {
+                                    throw new Error(
+                                        `Cache upload failed because file read failed with ${error.Message}`
+                                    );
+                                }),
                         start,
                         end
                     );