Skip to content

Commit

Permalink
Added custom error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
DanZfsd committed Jun 26, 2024
1 parent 45896ed commit 0227c79
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
20 changes: 16 additions & 4 deletions backend/typescript/rest/behaviourRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
BehaviourResponseDTO,
IBehaviourService,
} from "../services/interfaces/behaviourService";
import { getErrorMessage } from "../utilities/errorUtils";
import { getErrorMessage, NotFoundError } from "../utilities/errorUtils";
import { sendResponseByMimeType } from "../utilities/responseUtil";

const behaviourRouter: Router = Router();
Expand Down Expand Up @@ -52,7 +52,11 @@ behaviourRouter.get("/:id", async (req, res) => {
const behaviour = await behaviourService.getBehaviour(id);
res.status(200).json(behaviour);
} catch (e: unknown) {
res.status(500).send(getErrorMessage(e));
if (e instanceof NotFoundError) {
res.status(404).send(getErrorMessage(e));
} else {
res.status(500).send(getErrorMessage(e));
}
}
});

Expand All @@ -66,7 +70,11 @@ behaviourRouter.put("/:id", behaviourRequestDtoValidators, async (req, res) => {
});
res.status(200).json(behaviour);
} catch (e: unknown) {
res.status(500).send(getErrorMessage(e));
if (e instanceof NotFoundError) {
res.status(404).send(getErrorMessage(e));
} else {
res.status(500).send(getErrorMessage(e));
}
}
});

Expand All @@ -78,7 +86,11 @@ behaviourRouter.delete("/:id", async (req, res) => {
const deletedId = await behaviourService.deleteBehaviour(id);
res.status(200).json({ id: deletedId });
} catch (e: unknown) {
res.status(500).send(getErrorMessage(e));
if (e instanceof NotFoundError) {
res.status(404).send(getErrorMessage(e));
} else {
res.status(500).send(getErrorMessage(e));
}
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
BehaviourRequestDTO,
BehaviourResponseDTO,
} from "../interfaces/behaviourService";
import { getErrorMessage } from "../../utilities/errorUtils";
import { getErrorMessage, NotFoundError } from "../../utilities/errorUtils";
import logger from "../../utilities/logger";

const Logger = logger(__filename);
Expand All @@ -16,7 +16,7 @@ class BehaviourService implements IBehaviourService {
try {
behaviour = await PgBehaviour.findByPk(id, { raw: true });
if (!behaviour) {
throw new Error(`Behaviour id ${id} not found`);
throw new NotFoundError(`Behaviour id ${id} not found`);
}
} catch (error: unknown) {
Logger.error(
Expand Down Expand Up @@ -83,7 +83,7 @@ class BehaviourService implements IBehaviourService {
);

if (!updateResult[0]) {
throw new Error(`Behaviour id ${id} not found`);
throw new NotFoundError(`Behaviour id ${id} not found`);
}
[, [resultingBehaviour]] = updateResult;
} catch (error: unknown) {
Expand All @@ -104,7 +104,7 @@ class BehaviourService implements IBehaviourService {
where: { id },
});
if (!deleteResult) {
throw new Error(`Behaviour id ${id} not found`);
throw new NotFoundError(`Behaviour id ${id} not found`);
}
return id;
} catch (error: unknown) {
Expand Down
8 changes: 8 additions & 0 deletions backend/typescript/utilities/errorUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,11 @@
export const getErrorMessage = (error: unknown): string => {
return error instanceof Error ? error.message : "Unknown error occurred.";
};

// Thrown when resource is not found
export class NotFoundError extends Error {
constructor(message: string) {
super(message);
this.name = "NotFoundError";
}
}

0 comments on commit 0227c79

Please sign in to comment.