Skip to content

Commit

Permalink
feat: catch rate limiting error code, provide specific message (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans authored Feb 16, 2024
1 parent 3e0c3f4 commit d1fb1dd
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions apps/faucet/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,17 @@ export class API {
return response.json();
}
const reader = response?.body?.getReader();
const errors = reader
?.read()
.then(
(data): Promise<ErrorResponse> =>
Promise.reject(JSON.parse(new TextDecoder().decode(data.value)))
);
const errors = reader?.read().then((data): Promise<ErrorResponse> => {
const response = JSON.parse(
new TextDecoder().decode(data.value)
) as ErrorResponse;
// If code 429 is received on any request, rate limiting is blocking
// requests from this this IP, so provide a specific message:
if (response.code === 429) {
response.message = "Too many requests! Try again in one hour.";
}
return Promise.reject(response);
});
if (!errors) {
throw new Error("Unable to parse error response");
}
Expand Down
2 changes: 1 addition & 1 deletion apps/faucet/src/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ export type TransferResponse = {
};

export type ErrorResponse = {
code: string;
code: number;
message: string;
};

1 comment on commit d1fb1dd

@github-actions
Copy link

Choose a reason for hiding this comment

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

Please sign in to comment.