From 61d6c0370446c1252b2133b13cda46895b048aa4 Mon Sep 17 00:00:00 2001 From: Viet Huynh Date: Sat, 9 Sep 2023 21:24:02 +0700 Subject: [PATCH] handle API error --- lib/api.js | 8 ++++++-- lib/helpers.js | 4 ++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/api.js b/lib/api.js index bd5cbfd..628ec9e 100644 --- a/lib/api.js +++ b/lib/api.js @@ -63,8 +63,12 @@ const createZeroTrustListsAtOnce = async (items) => { listNumber++; } - await Promise.all(requests); - console.log(`Created ${totalListNumber} lists`); + try { + await Promise.all(requests); + console.log(`Created ${totalListNumber} lists`); + } catch (err) { + console.error(`Error occurred while creating lists - ${err.toString()}`); + } }; /** diff --git a/lib/helpers.js b/lib/helpers.js index 69ea096..8fd511a 100644 --- a/lib/helpers.js +++ b/lib/helpers.js @@ -17,6 +17,10 @@ const request = async (url, options) => { ...options, }); + if (!response.ok) { + throw new Error(`HTTP error! Status: ${response.status}`); + } + return response.json(); };