Skip to content

Commit

Permalink
add GuildAPIInvalidResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
cs-balazs committed Mar 13, 2024
1 parent 191e804 commit 7e03ec1
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 12 deletions.
38 changes: 37 additions & 1 deletion src/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,40 @@ class GuildSDKValidationError<Err extends ZodError<any>> 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 };
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export { default as createGuildClient, type GuildClient } from "./client";

This comment has been minimized.

Copy link
@Madzix91
export { GuildAPICallFailed, GuildSDKValidationError } from "./error";
export {
GuildAPICallFailed,
GuildAPIInvalidResponse,
GuildSDKValidationError,
} from "./error";
export { createSigner } from "./utils";

39 changes: 30 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}${
Expand Down Expand Up @@ -163,15 +167,16 @@ export const callGuildAPI = async <ResponseType>(

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
? {
Expand All @@ -191,7 +196,23 @@ export const callGuildAPI = async <ResponseType>(
},
});

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(
Expand Down

1 comment on commit 7e03ec1

@Sta0210
Copy link

@Sta0210 Sta0210 commented on 7e03ec1 Jan 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

Please sign in to comment.