Skip to content

Commit 1a07a23

Browse files
committed
removing unnecessary constants
1 parent 34ca632 commit 1a07a23

File tree

5 files changed

+27
-21
lines changed

5 files changed

+27
-21
lines changed

src/constants/responses.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,5 @@ export const NAME_CHANGED = "User nickname changed successfully";
2828

2929
export const ROLE_REMOVED = "Role Removed successfully";
3030

31-
export const ROLE_FETCH_FAILED_MESSAGE =
31+
export const ROLE_FETCH_FAILED =
3232
"Oops! We are experiencing an issue fetching roles.";
33-
34-
export const ROLE_FETCH_FAILED_ERROR = {
35-
error: ROLE_FETCH_FAILED_MESSAGE,
36-
};

src/controllers/guildRoleHandler.ts

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,13 @@ export async function getGuildRolesHandler(request: IRequest, env: env) {
8181
const roles = await getGuildRoles(env);
8282
return new JSONResponse({ roles });
8383
} catch (err: any) {
84-
if (err.message === response.ROLE_FETCH_FAILED_MESSAGE) {
85-
return new JSONResponse(response.ROLE_FETCH_FAILED_ERROR, {
86-
status: 500,
87-
});
84+
if (err.message === response.ROLE_FETCH_FAILED) {
85+
return new JSONResponse(
86+
{ error: response.ROLE_FETCH_FAILED },
87+
{
88+
status: 500,
89+
}
90+
);
8891
}
8992
return new JSONResponse(response.INTERNAL_SERVER_ERROR, { status: 500 });
9093
}
@@ -112,10 +115,13 @@ export async function getGuildRoleByRoleNameHandler(
112115
}
113116
return new JSONResponse(role);
114117
} catch (err: any) {
115-
if (err.message === response.ROLE_FETCH_FAILED_MESSAGE) {
116-
return new JSONResponse(response.ROLE_FETCH_FAILED_ERROR, {
117-
status: 500,
118-
});
118+
if (err.message === response.ROLE_FETCH_FAILED) {
119+
return new JSONResponse(
120+
{ error: response.ROLE_FETCH_FAILED },
121+
{
122+
status: 500,
123+
}
124+
);
119125
}
120126
return new JSONResponse(response.INTERNAL_SERVER_ERROR, { status: 500 });
121127
}

src/utils/guildRole.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
INTERNAL_SERVER_ERROR,
33
ROLE_ADDED,
44
ROLE_REMOVED,
5-
ROLE_FETCH_FAILED_MESSAGE,
5+
ROLE_FETCH_FAILED,
66
} from "../constants/responses";
77
import { DISCORD_BASE_URL } from "../constants/urls";
88
import { env } from "../typeDefinitions/default.types";
@@ -99,7 +99,7 @@ export async function getGuildRoles(env: env): Promise<Array<GuildRole>> {
9999
});
100100

101101
if (!response.ok) {
102-
throw new Error(ROLE_FETCH_FAILED_MESSAGE);
102+
throw new Error(ROLE_FETCH_FAILED);
103103
}
104104

105105
const guildDetails: GuildDetails = await response.json();

tests/unit/handlers/guildRoleHandler.test.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe("get roles", () => {
3838

3939
it("should return role fetch failed error response if it fails to fetch roles", async () => {
4040
getGuildRolesSpy.mockRejectedValueOnce({
41-
message: responseConstants.ROLE_FETCH_FAILED_MESSAGE,
41+
message: responseConstants.ROLE_FETCH_FAILED,
4242
});
4343
const mockRequest = generateDummyRequestObject({
4444
url: "/roles",
@@ -50,7 +50,9 @@ describe("get roles", () => {
5050
);
5151
const jsonResponse = await response.json();
5252
expect(response.status).toBe(500);
53-
expect(jsonResponse).toEqual(responseConstants.ROLE_FETCH_FAILED_ERROR);
53+
expect(jsonResponse).toEqual({
54+
error: responseConstants.ROLE_FETCH_FAILED,
55+
});
5456
});
5557

5658
it("should return internal server error response if it fails for any other reason", async () => {
@@ -189,7 +191,7 @@ describe("get role by role name", () => {
189191

190192
it("should return role fetch failed error if there was an error while fetching roles", async () => {
191193
getGuildRoleByNameSpy.mockRejectedValueOnce({
192-
message: responseConstants.ROLE_FETCH_FAILED_MESSAGE,
194+
message: responseConstants.ROLE_FETCH_FAILED,
193195
});
194196

195197
const mockRequest = generateDummyRequestObject({
@@ -206,7 +208,9 @@ describe("get role by role name", () => {
206208
);
207209
const jsonResponse: { roles: Array<GuildRole> } = await response.json();
208210
expect(response.status).toBe(500);
209-
expect(jsonResponse).toEqual(responseConstants.ROLE_FETCH_FAILED_ERROR);
211+
expect(jsonResponse).toEqual({
212+
error: responseConstants.ROLE_FETCH_FAILED,
213+
});
210214
});
211215

212216
it("should return internal server error if there was any other error", async () => {

tests/unit/utils/guildRole.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ describe("getGuildRoles", () => {
140140
Promise.resolve(new JSONResponse({}, { status: 500 }))
141141
);
142142
await expect(getGuildRoles(guildEnv)).rejects.toThrow(
143-
response.ROLE_FETCH_FAILED_MESSAGE
143+
response.ROLE_FETCH_FAILED
144144
);
145145
});
146146

@@ -173,7 +173,7 @@ describe("getGuildRolesByName", () => {
173173
Promise.resolve(new JSONResponse({}, { status: 500 }))
174174
);
175175
await expect(getGuildRoleByName("@everyone", guildEnv)).rejects.toThrow(
176-
response.ROLE_FETCH_FAILED_MESSAGE
176+
response.ROLE_FETCH_FAILED
177177
);
178178
});
179179

0 commit comments

Comments
 (0)