From c190e319a07e9f552b705471d9dd782f3aaad949 Mon Sep 17 00:00:00 2001 From: Laurin <60652077+Flexla54@users.noreply.github.com> Date: Wed, 7 Dec 2022 16:21:49 +0100 Subject: [PATCH] basic errorhandling inconsistancy fix --- src/Controllers/event.controller.ts | 53 +++++---------- src/Controllers/group.controllers.ts | 98 +++++++++++++--------------- 2 files changed, 59 insertions(+), 92 deletions(-) diff --git a/src/Controllers/event.controller.ts b/src/Controllers/event.controller.ts index 03fb8eb..7cbd4ac 100644 --- a/src/Controllers/event.controller.ts +++ b/src/Controllers/event.controller.ts @@ -72,47 +72,24 @@ export const getEvent = async (req: Request, res: Response) => { ); return; } - try { - const event = await prisma.event.findUnique({ - where: { - pid: eventId, - }, - select: detailedEvent, - }); - if (!event) { - throw new NotFoundError("event", eventId); - } + const event = await prisma.event.findUnique({ + where: { + pid: eventId, + }, + select: detailedEvent, + }); - res.status(200).json({ - type: "success", - payload: { - event, - }, - }); - } catch (e) { - if (e instanceof Prisma.PrismaClientKnownRequestError) { - return res.status(500).json({ - type: "error", - payload: { - message: `Internal Server error occured. Try again later`, - }, - }); - } - if (e instanceof Prisma.PrismaClientUnknownRequestError) { - return res.status(500).json({ - type: "error", - payload: { - message: "Unknown error occurred with your request. Check if your parameters are correct", - schema: { - eventId: DataType.UUID, - }, - }, - }); - } - - throw e; + if (!event) { + throw new NotFoundError("event", eventId); } + + res.status(200).json({ + type: "success", + payload: { + event, + }, + }); }; // requires: auth(ELEVATED) diff --git a/src/Controllers/group.controllers.ts b/src/Controllers/group.controllers.ts index c77ac6b..4437054 100644 --- a/src/Controllers/group.controllers.ts +++ b/src/Controllers/group.controllers.ts @@ -77,61 +77,51 @@ interface GetGroupQueryParams { export const getGroup = async (req: Request, res: Response) => { const { pid } = req.params; - try { - const group: { - pid: string; - name: string; - organisation: { pid: string; name: string }; - admins?: { pid: string; name: string }[]; - participants?: { pid: string }[]; - } | null = await prisma.group.findUnique({ - where: { pid }, - select: req.auth?.isAuthenticated - ? { - pid: true, - name: true, - organisation: { select: { pid: true, name: true } }, - admins: { select: { pid: true, name: true } }, - participants: { select: { pid: true } }, - } - : basicGroup, - }); - - if (!group) { - return res.status(404).json(generateError(`The group with ID '${pid}' could not be found`)); - } - - return res.status(200).json({ - type: "success", - payload: { - group: { - ...group, - organisation: { - ...group.organisation, - _links: [{ rel: "self", type: "GET", href: `/api/organisation/${group.organisation.pid}` }], - }, - ...(req.auth?.isAuthenticated - ? { - admins: group.admins?.map((admin) => ({ - ...admin, - _links: [{ rel: "self", type: "GET", href: `/api/admins/${admin.pid}` }], - })), - participants: group.participants?.map((participant) => ({ - ...participant, - _links: [{ rel: "self", type: "GET", href: `/api/participant/${participant.pid}` }], - })), - } - : {}), - }, - }, - }); - } catch (e) { - if (e instanceof PrismaClientUnknownRequestError) { - return res.status(400).json(generateError("Unknown error occured. This could be due to malformed IDs")); - } + const group: { + pid: string; + name: string; + organisation: { pid: string; name: string }; + admins?: { pid: string; name: string }[]; + participants?: { pid: string }[]; + } | null = await prisma.group.findUnique({ + where: { pid }, + select: req.auth?.isAuthenticated + ? { + pid: true, + name: true, + organisation: { select: { pid: true, name: true } }, + admins: { select: { pid: true, name: true } }, + participants: { select: { pid: true } }, + } + : basicGroup, + }); + if (!group) { + return res.status(404).json(generateError(`The group with ID '${pid}' could not be found`)); } - - return res.status(500).json(genericError); + return res.status(200).json({ + type: "success", + payload: { + group: { + ...group, + organisation: { + ...group.organisation, + _links: [{ rel: "self", type: "GET", href: `/api/organisation/${group.organisation.pid}` }], + }, + ...(req.auth?.isAuthenticated + ? { + admins: group.admins?.map((admin) => ({ + ...admin, + _links: [{ rel: "self", type: "GET", href: `/api/admins/${admin.pid}` }], + })), + participants: group.participants?.map((participant) => ({ + ...participant, + _links: [{ rel: "self", type: "GET", href: `/api/participant/${participant.pid}` }], + })), + } + : {}), + }, + }, + }); }; // at: POST /api/organisations/:eventPid/groups