From c16c7efbe6fb85d8cc08c046fbcb4e834c146495 Mon Sep 17 00:00:00 2001
From: Lu Zhu <misterzhu@gmail.com>
Date: Mon, 28 Feb 2022 10:31:58 -0800
Subject: [PATCH] Add promise rejection on error to upload-file (#37)

---
 utils/upload-file.js | 30 +++++++++++++++++-------------
 1 file changed, 17 insertions(+), 13 deletions(-)

diff --git a/utils/upload-file.js b/utils/upload-file.js
index 20d9978..22deac5 100644
--- a/utils/upload-file.js
+++ b/utils/upload-file.js
@@ -5,24 +5,28 @@ const uploadFile = (s3, params, cb = () => {}) => (new Promise((resolve, reject)
   const request = s3.putObject(params);
 
   request.on('complete', (response) => {
-    const { data: { ETag } } = response;
+    try {
+      const { data: { ETag } } = response;
 
-    const hash = ETag.replace(/^"|"$/g, '');
+      const hash = ETag.replace(/^"|"$/g, '');
 
-    const cidObj = CID.parse(hash);
-  
-    let cidv0;
-  
-    const cidv1 = cidObj.toV1().toString();
+      const cidObj = CID.parse(hash);
 
-    try {
-      cidv0 = cidObj.toV0().toString();
+      let cidv0;
+
+      const cidv1 = cidObj.toV1().toString();
+
+      try {
+        cidv0 = cidObj.toV0().toString();
+      } catch (e) {
+        // fallback when cbor is used
+        cidv0 = cidv1;
+      }
+
+      resolve ({ hash: cidv1, hashV0: cidv0 });
     } catch (e) {
-      // fallback when cbor is used
-      cidv0 = cidv1;
+      reject(e);
     }
-
-    resolve ({ hash: cidv1, hashV0: cidv0 });
   });
 
   request.on('error', (error) => {