From 7e03ec1905198dbb8d70e5941e3c43827d00e4f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Csete=20Bal=C3=A1zs?= Date: Wed, 13 Mar 2024 08:04:48 +0100 Subject: [PATCH] add GuildAPIInvalidResponse --- src/error.ts | 38 +++++++++++++++++++++++++++++++++++++- src/index.ts | 7 +++++-- src/utils.ts | 39 ++++++++++++++++++++++++++++++--------- 3 files changed, 72 insertions(+), 12 deletions(-) diff --git a/src/error.ts b/src/error.ts index 5a47103..bcd482a 100644 --- a/src/error.ts +++ b/src/error.ts @@ -43,4 +43,40 @@ class GuildSDKValidationError> extends Error { } } -export { GuildAPICallFailed, GuildSDKValidationError }; +class GuildAPIInvalidResponse extends Error { + responseText: string; + + responseCode: number; + + url: string; + + method: string; + + body: any; + + constructor({ + responseText, + response, + url, + method, + body, + }: { + responseText: string; + response: Response; + url: string; + method: string; + body: any; + }) { + super( + "Guild API returned invalid data. Please open an issue: https://github.com/guildxyz/guild-sdk/issues" + ); + + this.responseCode = response.status; + this.responseText = responseText; + this.url = url; + this.method = method; + this.body = body; + } +} + +export { GuildAPICallFailed, GuildAPIInvalidResponse, GuildSDKValidationError }; diff --git a/src/index.ts b/src/index.ts index 9d83837..d8b06ef 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,7 @@ export { default as createGuildClient, type GuildClient } from "./client"; -export { GuildAPICallFailed, GuildSDKValidationError } from "./error"; +export { + GuildAPICallFailed, + GuildAPIInvalidResponse, + GuildSDKValidationError, +} from "./error"; export { createSigner } from "./utils"; - diff --git a/src/utils.ts b/src/utils.ts index 1c6da4b..c9219b6 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,7 +4,11 @@ import { keccak256, type Wallet } from "ethers"; import randomBytes from "randombytes"; import type { z } from "zod"; import { globals } from "./common"; -import { GuildAPICallFailed, GuildSDKValidationError } from "./error"; +import { + GuildAPICallFailed, + GuildAPIInvalidResponse, + GuildSDKValidationError, +} from "./error"; export const recreateMessage = (params: Schemas["Authentication"]["params"]) => `${params.msg}\n\nAddress: ${params.addr}\nMethod: ${params.method}${ @@ -163,15 +167,16 @@ export const callGuildAPI = async ( const isPrivileged = "headers" in (authentication ?? {}); + const body = // eslint-disable-next-line no-nested-ternary + params.method === "GET" + ? undefined + : isPrivileged + ? JSON.stringify(parsedPayload) + : JSON.stringify(authentication ?? parsedPayload); + const response = await fetch(url, { method: params.method, - body: - // eslint-disable-next-line no-nested-ternary - params.method === "GET" - ? undefined - : isPrivileged - ? JSON.stringify(parsedPayload) - : JSON.stringify(authentication ?? parsedPayload), + body, headers: { ...(params.method === "GET" && authentication && !isPrivileged ? { @@ -191,7 +196,23 @@ export const callGuildAPI = async ( }, }); - const responseBody = await response.json(); + const responseBody = await response + .json() + .catch(() => "FAILED_TO_PARSE_RESPONSE_JSON"); + + if (responseBody === "FAILED_TO_PARSE_RESPONSE_JSON") { + const responseText = await response + .text() + .catch(() => "FAILED_TO_PARSE_RESPONSE_TEXT"); + + throw new GuildAPIInvalidResponse({ + responseText, + response, + url, + method: params.method, + body, + }); + } if (!response.ok) { throw new GuildAPICallFailed(