Skip to content

Commit 7010760

Browse files
committed
Throwing errors when something goes wrong
1 parent 86c08ce commit 7010760

File tree

3 files changed

+51
-47
lines changed

3 files changed

+51
-47
lines changed

src/controllers/guildRoleHandler.ts

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,16 @@ 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-
}
8582
return new JSONResponse({ roles });
8683
} catch (err: any) {
87-
if (err.message === response.ROLE_FETCH_FAILED) {
88-
return new JSONResponse(
89-
{ error: response.ROLE_FETCH_FAILED },
90-
{
91-
status: 500,
92-
headers: {
93-
"content-type": "application/json;charset=UTF-8",
94-
},
95-
}
96-
);
97-
}
84+
const error =
85+
err?.message === response.ROLE_FETCH_FAILED
86+
? response.ROLE_FETCH_FAILED
87+
: response.INTERNAL_SERVER_ERROR;
9888
return new JSONResponse(
99-
{ error: response.INTERNAL_SERVER_ERROR },
89+
{
90+
error,
91+
},
10092
{
10193
status: 500,
10294
headers: {
@@ -134,19 +126,12 @@ export async function getGuildRoleByRoleNameHandler(
134126
}
135127
return new JSONResponse(role);
136128
} catch (err: any) {
137-
if (err.message === response.ROLE_FETCH_FAILED) {
138-
return new JSONResponse(
139-
{ error: response.ROLE_FETCH_FAILED },
140-
{
141-
status: 500,
142-
headers: {
143-
"content-type": "application/json;charset=UTF-8",
144-
},
145-
}
146-
);
147-
}
129+
const error =
130+
err?.message === response.ROLE_FETCH_FAILED
131+
? response.ROLE_FETCH_FAILED
132+
: response.INTERNAL_SERVER_ERROR;
148133
return new JSONResponse(
149-
{ error: response.INTERNAL_SERVER_ERROR },
134+
{ error },
150135
{
151136
status: 500,
152137
headers: {

src/utils/guildRole.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
INTERNAL_SERVER_ERROR,
33
ROLE_ADDED,
4+
ROLE_FETCH_FAILED,
45
ROLE_REMOVED,
56
} from "../constants/responses";
67
import { DISCORD_BASE_URL } from "../constants/urls";
@@ -86,9 +87,7 @@ export async function removeGuildRole(details: memberGroupRole, env: env) {
8687
}
8788
}
8889

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

9493
try {
@@ -101,7 +100,7 @@ export async function getGuildRoles(
101100
});
102101

103102
if (!response.ok) {
104-
return undefined;
103+
throw new Error(ROLE_FETCH_FAILED);
105104
}
106105

107106
const guildDetails: GuildDetails = await response.json();
@@ -111,7 +110,7 @@ export async function getGuildRoles(
111110
name: role.name,
112111
}));
113112
} catch (err) {
114-
return undefined;
113+
throw new Error(ROLE_FETCH_FAILED);
115114
}
116115
}
117116

tests/unit/utils/guildRole.test.ts

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,30 @@ describe("removeGuildRole", () => {
132132
expect(result).toEqual(response.INTERNAL_SERVER_ERROR);
133133
});
134134
});
135+
135136
describe("getGuildRoles", () => {
136-
it("should return undefined if status is not ok", async () => {
137+
it("should throw role fetch failed error if status is not ok", async () => {
137138
jest
138139
.spyOn(global, "fetch")
139140
.mockImplementationOnce(async () =>
140141
Promise.resolve(new JSONResponse({}, { status: 500 }))
141142
);
142143

143-
const response = await getGuildRoles(guildEnv);
144-
expect(response).toBeUndefined();
144+
await expect(getGuildRoles(guildEnv)).rejects.toThrow(
145+
response.ROLE_FETCH_FAILED
146+
);
147+
});
148+
149+
it("should throw role fetch failed error if discord request fails", async () => {
150+
jest
151+
.spyOn(global, "fetch")
152+
.mockImplementationOnce(async () =>
153+
Promise.reject(new JSONResponse({}, { status: 500 }))
154+
);
155+
156+
await expect(getGuildRoles(guildEnv)).rejects.toThrow(
157+
response.ROLE_FETCH_FAILED
158+
);
145159
});
146160

147161
it("should return array of objects containing role_id and role_name", async () => {
@@ -151,16 +165,10 @@ describe("getGuildRoles", () => {
151165
Promise.resolve(new JSONResponse(guildDetailsMock))
152166
);
153167
const roles = await getGuildRoles(guildEnv);
154-
const expectedRoles = [
155-
{
156-
id: "1234567889",
157-
name: "@everyone",
158-
},
159-
{
160-
id: "12344567",
161-
name: "bot one",
162-
},
163-
];
168+
const expectedRoles = guildDetailsMock.roles.map(({ id, name }) => ({
169+
id,
170+
name,
171+
}));
164172
expect(roles).toEqual(expectedRoles);
165173
});
166174
});
@@ -172,8 +180,20 @@ describe("getGuildRolesByName", () => {
172180
.mockImplementationOnce(async () =>
173181
Promise.resolve(new JSONResponse({}, { status: 500 }))
174182
);
175-
const response = await getGuildRoleByName("@everyone", guildEnv);
176-
expect(response).toBeUndefined();
183+
await expect(getGuildRoles(guildEnv)).rejects.toThrow(
184+
response.ROLE_FETCH_FAILED
185+
);
186+
});
187+
188+
it("should throw role fetch failed message if discord request fails", async () => {
189+
jest
190+
.spyOn(global, "fetch")
191+
.mockImplementationOnce(async () =>
192+
Promise.reject(new JSONResponse({}, { status: 500 }))
193+
);
194+
await expect(getGuildRoles(guildEnv)).rejects.toThrow(
195+
response.ROLE_FETCH_FAILED
196+
);
177197
});
178198

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

0 commit comments

Comments
 (0)