mirror of
https://github.com/detleph/server.git
synced 2026-09-04 08:36:06 +02:00
Improve error handling and simplify code
This commit is contained in:
@@ -44,3 +44,19 @@ export const createInsufficientPermissionsError = (required: AdminLevel = "ELEVA
|
|||||||
},
|
},
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const generateError = (message: string) => {
|
||||||
|
return {
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const genericError = {
|
||||||
|
type: "error",
|
||||||
|
payload: {
|
||||||
|
message: "There was an error processing your request, please try again later",
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|||||||
@@ -1,7 +1,14 @@
|
|||||||
import { PrismaClientUnknownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientUnknownRequestError } from "@prisma/client/runtime";
|
||||||
import { Request, Response } from "express";
|
import { Request, Response } from "express";
|
||||||
import prisma from "../lib/prisma";
|
import prisma from "../lib/prisma";
|
||||||
import { AUTH_ERROR, createInsufficientPermissionsError, DataType, generateInvalidBodyError } from "./common";
|
import {
|
||||||
|
AUTH_ERROR,
|
||||||
|
createInsufficientPermissionsError,
|
||||||
|
DataType,
|
||||||
|
generateError,
|
||||||
|
generateInvalidBodyError,
|
||||||
|
genericError,
|
||||||
|
} from "./common";
|
||||||
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
import { PrismaClientKnownRequestError } from "@prisma/client/runtime";
|
||||||
import { Prisma } from "@prisma/client";
|
import { Prisma } from "@prisma/client";
|
||||||
|
|
||||||
@@ -230,13 +237,45 @@ export const updateOrganisation = async (
|
|||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof PrismaClientKnownRequestError) {
|
if (e instanceof PrismaClientKnownRequestError) {
|
||||||
if (e.code === "P2025") {
|
if (e.code === "P2025") {
|
||||||
return res.status(404).json({
|
return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`));
|
||||||
type: "error",
|
|
||||||
payload: {
|
|
||||||
message: `The organiation with the ID '${pid}' could not be found!`,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
} else if (e instanceof PrismaClientUnknownRequestError) {
|
||||||
|
return res.status(400).send(generateError("Unkonwn error occured. This could be due to malformed IDs"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return res.status(500).json(genericError);
|
||||||
|
};
|
||||||
|
|
||||||
|
interface DeleteOrganisationQueryParams {
|
||||||
|
pid: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
// requires: auth(ELEVATED)
|
||||||
|
export const deleteOrganisation = async (req: Request<DeleteOrganisationQueryParams>, res: Response) => {
|
||||||
|
if (!req.auth?.isAuthenticated) {
|
||||||
|
return res.status(500).json(AUTH_ERROR);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (req.auth.permission_level !== "ELEVATED") {
|
||||||
|
return res.status(403).json(createInsufficientPermissionsError());
|
||||||
|
}
|
||||||
|
|
||||||
|
const { pid } = req.params;
|
||||||
|
|
||||||
|
try {
|
||||||
|
prisma.organisation.delete({ where: { pid } });
|
||||||
|
|
||||||
|
res.status(204).end();
|
||||||
|
} catch (e) {
|
||||||
|
if (e instanceof PrismaClientKnownRequestError) {
|
||||||
|
if ((e.code = "P2025")) {
|
||||||
|
return res.status(404).json(generateError(`The organisation with the ID ${pid} could not be found`));
|
||||||
|
}
|
||||||
|
} else if (e instanceof PrismaClientUnknownRequestError) {
|
||||||
|
return res.status(400).send(generateError("Unkonwn error occured. This could be due to malformed IDs"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res.status(500).json(genericError);
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user