From 66ae4932486da12613b800a13f82498dfd063a3e Mon Sep 17 00:00:00 2001 From: Hunain Bin Sajid Date: Fri, 10 May 2024 18:26:58 +0500 Subject: [PATCH] feat: add error handler for browser api response (#7) * feat: add error handler for browser api response * fix: handle error rejection in requestAutoSignin --- index.js | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index e59c02e..13c57d1 100644 --- a/index.js +++ b/index.js @@ -77,11 +77,15 @@ const requestAutoSignin = async () => { subtype: "auto-signin-signature", }); if (error) { - window.postMessage({ type: "select-auto-signin", requestId }, "*"); - pubsub.subscribe(requestId, (_event, data) => { - resolve(data); - pubsub.unsubscribe(requestId); - }); + if (error.code === 404) { + window.postMessage({ type: "select-auto-signin", requestId }, "*"); + pubsub.subscribe(requestId, (_event, data) => { + resolve(data); + pubsub.unsubscribe(requestId); + }); + } else { + reject(error); + } } else { resolve(data); } @@ -105,11 +109,14 @@ const requestAutoSignin = async () => { const signifyFetch = async (url, req, fetchHeaders = false, aidName = "") => { if (fetchHeaders && aidName) { if (canCallAsync()) { - const { data } = await chrome.runtime.sendMessage(extensionId, { + const { data, error } = await chrome.runtime.sendMessage(extensionId, { type: "fetch-resource", subtype: "signify-headers", data: { aidName }, }); + if (error && error.message) { + throw new Error(error.message); + } req.headers = { ...(req.headers ?? {}), ...(data ?? {}) }; } else { req.headers = {