Skip to content

Commit

Permalink
fix: catch unknown id in router
Browse files Browse the repository at this point in the history
  • Loading branch information
Scalamando committed Nov 18, 2021
1 parent 2033ed2 commit 613a43b
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/controllers/digit.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ export default class DigitController {
}

@Get("/:id/jpeg")
public async getDigitAsJpeg(@Path() id: string): Promise<Buffer> {
public async getDigitAsJpeg(@Path() id: string): Promise<Buffer | null> {
const digit = await Repository.getDigit(Number(id));

if (!digit) {
throw Error("No digit with this id");
return null;
}

return digit.image;
Expand Down
1 change: 1 addition & 0 deletions src/routes/digit.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ router.get("/:id/jpeg", async (req: Request, res: Response) => {
const controller = new DigitController();
const response = await controller.getDigitAsJpeg(req.params.id);

if (!response) return res.status(404).send({ message: "No digit found" });
res.type("image/jpeg").send(response);
});

Expand Down

0 comments on commit 613a43b

Please sign in to comment.