mirror of
https://github.com/detleph/server.git
synced 2026-09-06 16:06:18 +02:00
Add better error handling
This commit is contained in:
@@ -5,6 +5,10 @@ import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInval
|
|||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
|
|
||||||
|
function validateOranisationName(name: string) {
|
||||||
|
return name.length > 0;
|
||||||
|
}
|
||||||
|
|
||||||
const detailedOrganisation = {
|
const detailedOrganisation = {
|
||||||
pid: true,
|
pid: true,
|
||||||
name: true,
|
name: true,
|
||||||
@@ -127,6 +131,15 @@ export const createOrganisation = async (
|
|||||||
return res.status(400).json(generateInvalidBodyError({ name: DataType.STRING, eventId: DataType.UUID }));
|
return res.status(400).json(generateInvalidBodyError({ name: DataType.STRING, eventId: DataType.UUID }));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!validateOranisationName(name)) {
|
||||||
|
return res.status(400).json({
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: "The name has to be at least 1 character long",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
// Check if event exists
|
// Check if event exists
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -184,7 +197,7 @@ export const updateOrganisation = async (
|
|||||||
const { pid } = req.params;
|
const { pid } = req.params;
|
||||||
const { name } = req.body;
|
const { name } = req.body;
|
||||||
|
|
||||||
if (name && typeof name !== "string") {
|
if (name !== undefined && typeof name !== "string") {
|
||||||
return res.status(400).json(
|
return res.status(400).json(
|
||||||
generateInvalidBodyError({
|
generateInvalidBodyError({
|
||||||
name: DataType.STRING,
|
name: DataType.STRING,
|
||||||
@@ -192,16 +205,38 @@ export const updateOrganisation = async (
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const organisation = await prisma.organisation.update({
|
if (name !== undefined && !validateOranisationName(name)) {
|
||||||
where: { pid },
|
return res.status(400).json({
|
||||||
data: { name },
|
type: "error",
|
||||||
select: detailedOrganisation,
|
payload: {
|
||||||
});
|
message: "The name has to be at least 1 character long",
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return res.status(200).json({
|
try {
|
||||||
type: "success",
|
const organisation = await prisma.organisation.update({
|
||||||
payload: {
|
where: { pid },
|
||||||
organisation,
|
data: { name },
|
||||||
},
|
select: detailedOrganisation,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return res.status(200).json({
|
||||||
|
type: "success",
|
||||||
|
payload: {
|
||||||
|
organisation,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof PrismaClientKnownRequestError) {
|
||||||
|
if (e.code === "P2025") {
|
||||||
|
return res.status(404).json({
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: `The organiation with the ID '${pid}' could not be found!`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user