basic errorhandling inconsistancy fix

This commit is contained in:
Laurin
2023-01-02 18:56:12 +01:00
committed by La_Felx
parent b9278b56df
commit c190e319a0
2 changed files with 59 additions and 92 deletions
+15 -38
View File
@@ -72,47 +72,24 @@ export const getEvent = async (req: Request, res: Response) => {
); );
return; return;
} }
try {
const event = await prisma.event.findUnique({
where: {
pid: eventId,
},
select: detailedEvent,
});
if (!event) { const event = await prisma.event.findUnique({
throw new NotFoundError("event", eventId); where: {
} pid: eventId,
},
select: detailedEvent,
});
res.status(200).json({ if (!event) {
type: "success", throw new NotFoundError("event", eventId);
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;
} }
res.status(200).json({
type: "success",
payload: {
event,
},
});
}; };
// requires: auth(ELEVATED) // requires: auth(ELEVATED)
+44 -54
View File
@@ -77,61 +77,51 @@ interface GetGroupQueryParams {
export const getGroup = async (req: Request<GetGroupQueryParams>, res: Response) => { export const getGroup = async (req: Request<GetGroupQueryParams>, res: Response) => {
const { pid } = req.params; const { pid } = req.params;
try { const group: {
const group: { pid: string;
pid: string; name: string;
name: string; organisation: { pid: string; name: string };
organisation: { pid: string; name: string }; admins?: { pid: string; name: string }[];
admins?: { pid: string; name: string }[]; participants?: { pid: string }[];
participants?: { pid: string }[]; } | null = await prisma.group.findUnique({
} | null = await prisma.group.findUnique({ where: { pid },
where: { pid }, select: req.auth?.isAuthenticated
select: req.auth?.isAuthenticated ? {
? { pid: true,
pid: true, name: true,
name: true, organisation: { select: { pid: true, name: true } },
organisation: { select: { pid: true, name: true } }, admins: { select: { pid: true, name: true } },
admins: { select: { pid: true, name: true } }, participants: { select: { pid: true } },
participants: { select: { pid: true } }, }
} : basicGroup,
: basicGroup, });
}); if (!group) {
return res.status(404).json(generateError(`The group with ID '${pid}' could not be found`));
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"));
}
} }
return res.status(200).json({
return res.status(500).json(genericError); 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 // at: POST /api/organisations/:eventPid/groups