Skip to content

Commit

Permalink
update api
Browse files Browse the repository at this point in the history
  • Loading branch information
iainnash committed Oct 22, 2023
1 parent 9e93204 commit 654998a
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions packages/premint-sdk/src/premint-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ export const networkConfigByChain: Record<number, NetworkConfig> = {

export class BadResponse extends Error {
status: number;
constructor(message: string, status: number) {
json: any;
constructor(message: string, status: number, json: any) {
super(message);
this.name = "BadResponse";
this.status = status;
this.json = json;
}
}

Expand Down Expand Up @@ -227,7 +229,15 @@ export class PremintAPI {
async get(path: string) {
const response = await fetch(path, { method: "GET" });
if (response.status !== 200) {
throw new Error(`Invalid response, status ${response.status}`);
let json;
try {
json = await response.json();
} catch (e: any) {}
throw new BadResponse(
`Invalid response, status ${response.status}`,
response.status,
json,
);
}
return await response.json();
}
Expand All @@ -251,7 +261,15 @@ export class PremintAPI {
body: JSON.stringify(data),
});
if (response.status !== 200) {
throw new Error(`Bad response: ${response.status}`);
let json;
try {
json = await response.json();
} catch (e: any) {}
throw new BadResponse(
`Bad response: ${response.status}`,
response.status,
json,
);
}
return await response.json();
}
Expand Down

0 comments on commit 654998a

Please sign in to comment.