From 15d0d178d6d69aaa9d7b49ff26bc221482e6313a Mon Sep 17 00:00:00 2001 From: Mel O'Hagan Date: Tue, 30 Aug 2022 15:18:44 +0100 Subject: [PATCH] Handle non-csv file error --- packages/server/src/integrations/s3.ts | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/server/src/integrations/s3.ts b/packages/server/src/integrations/s3.ts index 43aa04de6a..eb3605af8f 100644 --- a/packages/server/src/integrations/s3.ts +++ b/packages/server/src/integrations/s3.ts @@ -130,14 +130,25 @@ module S3Module { }) .createReadStream() - return new Promise((resolve, reject) => { - stream - .on("error", (err: Error) => { - reject(err) - }) - .on("finish", async () => { - resolve(csv().fromStream(stream)) + let csvError = false + return new Promise(async (resolve, reject) => { + stream.on("error", (err: Error) => { + reject(err) + }) + const response = csv() + .fromStream(stream) + .on("error", () => { + csvError = true }) + stream.on("finish", () => { + resolve(response) + }) + }).catch(err => { + if (csvError) { + throw new Error("Could not read CSV") + } else { + throw err + } }) } }