From 34acb14a04337631f750199b89a7f4e673b6de20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Danjou?= Date: Wed, 4 Dec 2024 01:52:51 +0100 Subject: [PATCH] feat: add more granularity on errors in reencrypt --- src/sdk/reencrypt.ts | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/sdk/reencrypt.ts b/src/sdk/reencrypt.ts index 3e49697..e2a008a 100644 --- a/src/sdk/reencrypt.ts +++ b/src/sdk/reencrypt.ts @@ -63,16 +63,32 @@ export const reencryptRequest = throw new Error('Invalid public or private key', { cause: e }); } + let response; let json; try { - const response = await fetch(`${gatewayUrl}reencrypt`, options); + response = await fetch(`${gatewayUrl}reencrypt`, options); + if (!response.ok) { + throw new Error( + `Reencrypt failed: gateway respond with HTTP code ${response.status}`, + ); + } + } catch (e) { + throw new Error("Reencrypt failed: Gateway didn't respond", { cause: e }); + } + + try { json = await response.json(); } catch (e) { - throw new Error("Gateway didn't response correctly", { cause: e }); + throw new Error("Reencrypt failed: Gateway didn't return a JSON", { + cause: e, + }); } if (json.status === 'failure') { - throw new Error("The reencryption didn't succeed", { cause: json }); + throw new Error( + "Reencrypt failed: the reencryption didn't succeed for an unknown reason", + { cause: json }, + ); } const client = new_client(kmsSignatures, userAddress, 'default');