Skip to content

Commit

Permalink
throw for http error
Browse files Browse the repository at this point in the history
  • Loading branch information
rithviknishad committed May 18, 2024
1 parent ddeee25 commit 2a584d4
Showing 1 changed file with 30 additions and 15 deletions.
45 changes: 30 additions & 15 deletions activities-bot/src/utils.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ function isAllowedEvent(event) {
}
}

const throwForHttpError = async (res) => {
if (!res.ok) {
throw new Error(await res.text());
}

return res;
};

const octokit = new Octokit({ auth: GITHUB_TOKEN });

export async function getEvents(allowedAuthors) {
Expand Down Expand Up @@ -112,17 +120,22 @@ const leaderboardApiHeaders = {
const eodUpdatesApi = `${LEADERBOARD_URL}/api/slack-eod-bot/eod-updates`;

export async function getEODUpdates() {
const res = await fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
});
const res = throwForHttpError(
await fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
}),
);

return res.json();
}

export async function flushEODUpdates() {
await fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
method: "DELETE",
});
const res = throwForHttpError(
await fetch(eodUpdatesApi, {
headers: leaderboardApiHeaders,
method: "DELETE",
}),
);
}

const slackApiHeaders = {
Expand All @@ -131,15 +144,17 @@ const slackApiHeaders = {
};

export async function sendSlackMessage(channel, text, blocks) {
const res = await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
headers: slackApiHeaders,
body: JSON.stringify({
channel,
text,
...blocks,
const res = throwForHttpError(
await fetch("https://slack.com/api/chat.postMessage", {
method: "POST",
headers: slackApiHeaders,
body: JSON.stringify({
channel,
text,
...blocks,
}),
}),
});
);

const data = await res.json();
if (!data.ok) {
Expand Down

0 comments on commit 2a584d4

Please sign in to comment.