Skip to content

Commit 86c08ce

Browse files
committed
Adding extra try blocks in utils to handle errors
1 parent aaab327 commit 86c08ce

File tree

3 files changed

+32
-25
lines changed

3 files changed

+32
-25
lines changed

src/controllers/guildRoleHandler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,9 @@ export async function getGuildRolesHandler(request: IRequest, env: env) {
7979
try {
8080
await verifyAuthToken(authHeader, env);
8181
const roles = await getGuildRoles(env);
82+
if (!roles) {
83+
throw new Error(response.ROLE_FETCH_FAILED);
84+
}
8285
return new JSONResponse({ roles });
8386
} catch (err: any) {
8487
if (err.message === response.ROLE_FETCH_FAILED) {

src/utils/guildRole.ts

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import {
22
INTERNAL_SERVER_ERROR,
33
ROLE_ADDED,
44
ROLE_REMOVED,
5-
ROLE_FETCH_FAILED,
65
} from "../constants/responses";
76
import { DISCORD_BASE_URL } from "../constants/urls";
87
import { env } from "../typeDefinitions/default.types";
@@ -87,33 +86,39 @@ export async function removeGuildRole(details: memberGroupRole, env: env) {
8786
}
8887
}
8988

90-
export async function getGuildRoles(env: env): Promise<Array<GuildRole>> {
89+
export async function getGuildRoles(
90+
env: env
91+
): Promise<Array<GuildRole> | undefined> {
9192
const guildDetailsUrl = `${DISCORD_BASE_URL}/guilds/${env.DISCORD_GUILD_ID}`;
9293

93-
const response = await fetch(guildDetailsUrl, {
94-
method: "GET",
95-
headers: {
96-
"Content-Type": "application/json",
97-
Authorization: `Bot ${env.DISCORD_TOKEN}`,
98-
},
99-
});
94+
try {
95+
const response = await fetch(guildDetailsUrl, {
96+
method: "GET",
97+
headers: {
98+
"Content-Type": "application/json",
99+
Authorization: `Bot ${env.DISCORD_TOKEN}`,
100+
},
101+
});
100102

101-
if (!response.ok) {
102-
throw new Error(ROLE_FETCH_FAILED);
103-
}
103+
if (!response.ok) {
104+
return undefined;
105+
}
104106

105-
const guildDetails: GuildDetails = await response.json();
107+
const guildDetails: GuildDetails = await response.json();
106108

107-
return guildDetails.roles.map((role) => ({
108-
id: role.id,
109-
name: role.name,
110-
}));
109+
return guildDetails.roles.map((role) => ({
110+
id: role.id,
111+
name: role.name,
112+
}));
113+
} catch (err) {
114+
return undefined;
115+
}
111116
}
112117

113118
export async function getGuildRoleByName(
114119
roleName: string,
115120
env: env
116121
): Promise<GuildRole | undefined> {
117122
const roles = await getGuildRoles(env);
118-
return roles.find((role) => role.name === roleName);
123+
return roles?.find((role) => role.name === roleName);
119124
}

tests/unit/utils/guildRole.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,15 +133,15 @@ describe("removeGuildRole", () => {
133133
});
134134
});
135135
describe("getGuildRoles", () => {
136-
it("should throw role fetch failed message if status is not ok", async () => {
136+
it("should return undefined if status is not ok", async () => {
137137
jest
138138
.spyOn(global, "fetch")
139139
.mockImplementationOnce(async () =>
140140
Promise.resolve(new JSONResponse({}, { status: 500 }))
141141
);
142-
await expect(getGuildRoles(guildEnv)).rejects.toThrow(
143-
response.ROLE_FETCH_FAILED
144-
);
142+
143+
const response = await getGuildRoles(guildEnv);
144+
expect(response).toBeUndefined();
145145
});
146146

147147
it("should return array of objects containing role_id and role_name", async () => {
@@ -172,9 +172,8 @@ describe("getGuildRolesByName", () => {
172172
.mockImplementationOnce(async () =>
173173
Promise.resolve(new JSONResponse({}, { status: 500 }))
174174
);
175-
await expect(getGuildRoleByName("@everyone", guildEnv)).rejects.toThrow(
176-
response.ROLE_FETCH_FAILED
177-
);
175+
const response = await getGuildRoleByName("@everyone", guildEnv);
176+
expect(response).toBeUndefined();
178177
});
179178

180179
it("should return array of objects containing role_id and role_name", async () => {

0 commit comments

Comments
 (0)