Skip to content

Commit

Permalink
removeprefixes from error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Turner authored and Ben Turner committed Mar 9, 2024
1 parent 25833e5 commit 0781479
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 20 deletions.
4 changes: 1 addition & 3 deletions src/routers/qr-code.router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ describe("qr-code-router.ts", () => {
const response = await mockServer().get("/api/qr-code/generate");
expect(response.status).toBe(500);
expect(getLocationCoordinatesSpy).toHaveBeenCalledTimes(1);
expect(response.body.message).toEqual(
"InvalidLocation: Invalid location of request",
);
expect(response.body.message).toEqual("Invalid location of request");
});
});

Expand Down
3 changes: 1 addition & 2 deletions src/routers/qr-code.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ qrCodeRouter.get("/generate", async (req, res, next) => {
const ip = parseIp(req);
if (isProductionEnv()) {
const coords = await getLocationCoordinates(ip as string);
if (!validateCoords(coords))
throw Error("InvalidLocation: Invalid location of request");
if (!validateCoords(coords)) throw Error("Invalid location of request");
}
const accessToken = generateAccessToken();
const payload = encodeURI(
Expand Down
8 changes: 3 additions & 5 deletions src/services/attendance.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ describe("attendance.service.test.ts", () => {
} catch (err) {
const { message, name } = err as Error;
expect(name).toBe("Error");
expect(message).toBe("InvalidCheckout: No entries found");
expect(message).toBe("No entries found");
}
});
it("should throw InvalidCheckout error if last entry has an end date", async () => {
it("should throw error if last entry has an end date", async () => {
jest.spyOn(EntryModel, "find").mockImplementationOnce(
jest.fn().mockReturnValue({
sort: jest.fn().mockResolvedValue([{ end: new Date() }]),
Expand All @@ -64,9 +64,7 @@ describe("attendance.service.test.ts", () => {
} catch (err) {
const { message, name } = err as Error;
expect(name).toBe("Error");
expect(message).toContain(
"InvalidCheckout: Latest entry already has checked out at",
);
expect(message).toContain("Latest entry already has checked out at");
}
});

Expand Down
6 changes: 2 additions & 4 deletions src/services/attendance.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@ export const checkOut = async (payload: EntryPayload) => {
createdAt: "desc",
});
const latest = entries[0];
if (!latest) throw Error("InvalidCheckout: No entries found");
if (!latest) throw Error("No entries found");
if (latest.end)
throw Error(
"InvalidCheckout: Latest entry already has checked out at " + latest.end,
);
throw Error("Latest entry already has checked out at " + latest.end);
latest.end = new Date();
return await latest.save();
};
Expand Down
4 changes: 2 additions & 2 deletions src/services/auth.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("auth.service.ts", () => {
});
expect(success).toBe(false);
} catch (err) {
expect((err as Error).message).toBe("Unauthorized: No user found");
expect((err as Error).message).toBe("No user found");
}
});

Expand All @@ -45,7 +45,7 @@ describe("auth.service.ts", () => {
});
expect(success).toBe(false);
} catch (err) {
expect((err as Error).message).toBe("Unauthorized: Invalid password");
expect((err as Error).message).toBe("Invalid password");
expect(mockUpdate).toHaveBeenCalledWith("invalid-password", "utf8");
}
});
Expand Down
5 changes: 2 additions & 3 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@ export const generateAdminAccessToken = () => {

export const validateAdminPassword = async (payload: AdminLoginPayload) => {
const adminUser = await UserModel.findOne({ username: payload.username });
if (!adminUser) throw Error("Unauthorized: No user found");
if (!adminUser) throw Error("No user found");
const hash = createHash("sha256")
.update(payload.password, "utf8")
.digest("hex");
if (hash !== adminUser.password)
throw Error("Unauthorized: Invalid password");
if (hash !== adminUser.password) throw Error("Invalid password");
return { success: true };
};
2 changes: 1 addition & 1 deletion src/services/qr-code.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const decodeQRCodeDataUrl = async (

return decoded.data;
} catch (err) {
console.error(Error("Error: QR Decoding failed"));
console.error(Error("QR Decoding failed"));
console.error(err);
}
};
Expand Down

0 comments on commit 0781479

Please sign in to comment.