efficiency and exception change

This commit is contained in:
Julian Krauser 2024-09-06 10:08:19 +02:00
parent 4048c21c1f
commit 88212ff79b
32 changed files with 121 additions and 124 deletions

View file

@ -22,7 +22,7 @@ export default abstract class AwardCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating award");
throw new InternalException("Failed creating award", err);
});
}
@ -42,7 +42,7 @@ export default abstract class AwardCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating award");
throw new InternalException("Failed updating award", err);
});
}
@ -60,7 +60,7 @@ export default abstract class AwardCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting award");
throw new InternalException("Failed deleting award", err);
});
}
}

View file

@ -27,7 +27,7 @@ export default abstract class CommunicationTypeCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating communicationType");
throw new InternalException("Failed creating communicationType", err);
});
}
@ -48,7 +48,7 @@ export default abstract class CommunicationTypeCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating communicationType");
throw new InternalException("Failed updating communicationType", err);
});
}
@ -66,7 +66,7 @@ export default abstract class CommunicationTypeCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting communicationType");
throw new InternalException("Failed deleting communicationType", err);
});
}
}

View file

@ -26,7 +26,7 @@ export default abstract class ExecutivePositionCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating executivePosition");
throw new InternalException("Failed creating executivePosition", err);
});
}
@ -46,7 +46,7 @@ export default abstract class ExecutivePositionCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating executivePosition");
throw new InternalException("Failed updating executivePosition", err);
});
}
@ -64,7 +64,7 @@ export default abstract class ExecutivePositionCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting executivePosition");
throw new InternalException("Failed deleting executivePosition", err);
});
}
}

View file

@ -31,7 +31,7 @@ export default abstract class InviteCommandHandler {
return token;
})
.catch((err) => {
throw new InternalException("Failed saving invite");
throw new InternalException("Failed saving invite", err);
});
}
@ -50,7 +50,7 @@ export default abstract class InviteCommandHandler {
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed invite removal");
throw new InternalException("failed invite removal", err);
});
}
}

View file

@ -26,7 +26,7 @@ export default abstract class MembershipStatusCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating membershipStatus");
throw new InternalException("Failed creating membershipStatus", err);
});
}
@ -46,7 +46,7 @@ export default abstract class MembershipStatusCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating membershipStatus");
throw new InternalException("Failed updating membershipStatus", err);
});
}
@ -64,7 +64,7 @@ export default abstract class MembershipStatusCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting membershipStatus");
throw new InternalException("Failed deleting membershipStatus", err);
});
}
}

View file

@ -27,7 +27,7 @@ export default abstract class QualificationCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating qualification");
throw new InternalException("Failed creating qualification", err);
});
}
@ -48,7 +48,7 @@ export default abstract class QualificationCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating qualification");
throw new InternalException("Failed updating qualification", err);
});
}
@ -66,7 +66,7 @@ export default abstract class QualificationCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting qualification");
throw new InternalException("Failed deleting qualification", err);
});
}
}

View file

@ -36,7 +36,7 @@ export default abstract class RefreshCommandHandler {
return refreshToken;
})
.catch((err) => {
throw new InternalException("Failed saving refresh token");
throw new InternalException("Failed saving refresh token", err);
});
}
@ -55,7 +55,7 @@ export default abstract class RefreshCommandHandler {
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed refresh removal");
throw new InternalException("failed refresh removal", err);
});
}
@ -72,7 +72,7 @@ export default abstract class RefreshCommandHandler {
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed expired refresh removal");
throw new InternalException("failed expired refresh removal", err);
});
}
}

View file

@ -22,7 +22,7 @@ export default abstract class RoleCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating role");
throw new InternalException("Failed creating role", err);
});
}
@ -42,7 +42,7 @@ export default abstract class RoleCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating role");
throw new InternalException("Failed updating role", err);
});
}
@ -60,7 +60,7 @@ export default abstract class RoleCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting role");
throw new InternalException("Failed deleting role", err);
});
}
}

View file

@ -28,47 +28,45 @@ export default abstract class RolePermissionCommandHandler {
let newPermissions = PermissionHelper.getWhatToAdd(currentPermissions, updateRolePermissions.permissions);
let removePermissions = PermissionHelper.getWhatToRemove(currentPermissions, updateRolePermissions.permissions);
for (let permission of newPermissions) {
await this.updatePermissionsAdd(manager, updateRolePermissions.roleId, permission);
}
for (let permission of removePermissions) {
await this.updatePermissionsRemove(manager, updateRolePermissions.roleId, permission);
}
await this.updatePermissionsAdd(manager, updateRolePermissions.roleId, newPermissions);
await this.updatePermissionsRemove(manager, updateRolePermissions.roleId, removePermissions);
})
.then(() => {})
.catch((err) => {
throw new InternalException("Failed saving role permissions");
throw new InternalException("Failed saving role permissions", err);
});
}
private static async updatePermissionsAdd(
manager: EntityManager,
roleId: number,
permission: PermissionString
permissions: Array<PermissionString>
): Promise<InsertResult> {
return await manager
.createQueryBuilder()
.insert()
.into(rolePermission)
.values({
permission: permission,
roleId: roleId,
})
.values(
permissions.map((p) => ({
permission: p,
roleId: roleId,
}))
)
.orIgnore()
.execute();
}
private static async updatePermissionsRemove(
manager: EntityManager,
roleId: number,
permission: PermissionString
permissions: Array<PermissionString>
): Promise<DeleteResult> {
return await manager
.createQueryBuilder()
.delete()
.from(rolePermission)
.where("userId = :id", { id: roleId })
.andWhere("permission = :permission", { permission: permission })
.where("roleId = :id", { id: roleId })
.andWhere("permission IN (:...permission)", { permission: permissions })
.execute();
}
@ -91,7 +89,7 @@ export default abstract class RolePermissionCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed saving role permission");
throw new InternalException("Failed saving role permission", err);
});
}
@ -110,7 +108,7 @@ export default abstract class RolePermissionCommandHandler {
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed role permission removal");
throw new InternalException("failed role permission removal", err);
});
}
}

View file

@ -28,7 +28,7 @@ export default abstract class UserCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed saving user");
throw new InternalException("Failed saving user", err);
});
}
@ -51,7 +51,7 @@ export default abstract class UserCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed updating user");
throw new InternalException("Failed updating user", err);
});
}
@ -77,7 +77,7 @@ export default abstract class UserCommandHandler {
})
.then(() => {})
.catch((err) => {
throw new InternalException("Failed saving user roles");
throw new InternalException("Failed saving user roles", err);
});
}
@ -103,7 +103,7 @@ export default abstract class UserCommandHandler {
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed deleting user");
throw new InternalException("Failed deleting user", err);
});
}
}

View file

@ -27,48 +27,45 @@ export default abstract class UserPermissionCommandHandler {
let newPermissions = PermissionHelper.getWhatToAdd(currentPermissions, updateUserPermissions.permissions);
let removePermissions = PermissionHelper.getWhatToRemove(currentPermissions, updateUserPermissions.permissions);
for (let permission of newPermissions) {
await this.updatePermissionsAdd(manager, updateUserPermissions.userId, permission);
}
for (let permission of removePermissions) {
await this.updatePermissionsRemove(manager, updateUserPermissions.userId, permission);
}
await this.updatePermissionsAdd(manager, updateUserPermissions.userId, newPermissions);
await this.updatePermissionsRemove(manager, updateUserPermissions.userId, removePermissions);
})
.then(() => {})
.catch((err) => {
console.log(err);
throw new InternalException("Failed saving user permissions");
throw new InternalException("Failed saving user permissions", err);
});
}
private static async updatePermissionsAdd(
manager: EntityManager,
userId: number,
permission: PermissionString
permissions: Array<PermissionString>
): Promise<InsertResult> {
return await manager
.createQueryBuilder()
.insert()
.into(userPermission)
.values({
permission: permission,
userId: userId,
})
.values(
permissions.map((p) => ({
permission: p,
userId: userId,
}))
)
.orIgnore()
.execute();
}
private static async updatePermissionsRemove(
manager: EntityManager,
userId: number,
permission: PermissionString
permissions: Array<PermissionString>
): Promise<DeleteResult> {
return await manager
.createQueryBuilder()
.delete()
.from(userPermission)
.where("userId = :id", { id: userId })
.andWhere("permission = :permission", { permission: permission })
.andWhere("permission IN (:...permission)", { permission: permissions })
.execute();
}
@ -91,7 +88,7 @@ export default abstract class UserPermissionCommandHandler {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed saving user permission");
throw new InternalException("Failed saving user permission", err);
});
}
@ -110,7 +107,7 @@ export default abstract class UserPermissionCommandHandler {
.execute()
.then((res) => {})
.catch((err) => {
throw new InternalException("failed user permission removal");
throw new InternalException("failed user permission removal", err);
});
}
}

View file

@ -60,7 +60,7 @@ export async function login(req: Request, res: Response): Promise<any> {
})
.catch((err) => {
console.log(err);
throw new InternalException("Failed accessToken creation");
throw new InternalException("Failed accessToken creation", err);
});
let refreshCommand: CreateRefreshCommand = {
@ -128,8 +128,7 @@ export async function refresh(req: Request, res: Response): Promise<any> {
accessToken = result;
})
.catch((err) => {
console.log(err);
throw new InternalException("Failed accessToken creation");
throw new InternalException("Failed accessToken creation", err);
});
let refreshCommand: CreateRefreshCommand = {

View file

@ -90,7 +90,7 @@ export async function verifyInvite(req: Request, res: Response): Promise<any> {
});
})
.catch((err) => {
throw new InternalException("QRCode not created");
throw new InternalException("QRCode not created", err);
});
}
@ -154,8 +154,7 @@ export async function finishInvite(req: Request, res: Response, grantAdmin: bool
accessToken = result;
})
.catch((err) => {
console.log(err);
throw new InternalException("Failed accessToken creation");
throw new InternalException("Failed accessToken creation", err);
});
let refreshCommand: CreateRefreshCommand = {

View file

@ -1,7 +1,7 @@
import CustomRequestException from "./customRequestException";
export default class BadRequestException extends CustomRequestException {
constructor(msg: string) {
super(400, msg);
constructor(msg: string, err?: any) {
super(400, msg, err);
}
}

View file

@ -2,9 +2,11 @@ import { ExceptionBase } from "./exceptionsBaseType";
export default class CustomRequestException extends Error implements ExceptionBase {
statusCode: number;
err?: any;
constructor(status: number, msg: string) {
constructor(status: number, msg: string, err?: any) {
super(msg);
this.statusCode = status;
this.err = err;
}
}

View file

@ -1,7 +1,7 @@
import CustomRequestException from "./customRequestException";
export default class ForbiddenRequestException extends CustomRequestException {
constructor(msg: string) {
super(403, msg);
constructor(msg: string, err?: any) {
super(403, msg, err);
}
}

View file

@ -1,7 +1,7 @@
import CustomRequestException from "./customRequestException";
export default class InternalException extends CustomRequestException {
constructor(msg: string) {
super(500, msg);
constructor(msg: string, err?: any) {
super(500, msg, err);
}
}

View file

@ -1,7 +1,7 @@
import CustomRequestException from "./customRequestException";
export default class UnauthorizedRequestException extends CustomRequestException {
constructor(msg: string) {
super(401, msg);
constructor(msg: string, err?: any) {
super(401, msg, err);
}
}

View file

@ -19,9 +19,9 @@ export default async function authenticate(req: Request, res: Response, next: Fu
})
.catch((err) => {
if (err == "jwt expired") {
throw new UnauthorizedRequestException("Token expired");
throw new UnauthorizedRequestException("Token expired", err);
} else {
throw new BadRequestException("Failed Authorization Header decoding");
throw new BadRequestException("Failed Authorization Header decoding", err);
}
});

View file

@ -5,13 +5,15 @@ import CustomRequestException from "../exceptions/customRequestException";
export default function errorHandler(err: ExceptionBase | Error, req: Request, res: Response, next: Function) {
let status = 500;
let msg = "Internal Server Error";
let insideError = undefined;
if (err instanceof CustomRequestException) {
status = err.statusCode;
msg = err.message;
insideError = err.err;
}
console.log("Handler", err);
console.log("Handler", err, insideError);
res.status(status).send(msg);
}

View file

@ -16,8 +16,8 @@ export default abstract class AwardService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("awards not found");
.catch((err) => {
throw new InternalException("awards not found", err);
});
}
@ -34,8 +34,8 @@ export default abstract class AwardService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("award not found by id");
.catch((err) => {
throw new InternalException("award not found by id", err);
});
}
@ -53,8 +53,8 @@ export default abstract class AwardService {
// .then((res) => {
// return [];
// })
// .catch(() => {
// throw new InternalException("award assigned members not found by id");
// .catch((err) => {
// throw new InternalException("award assigned members not found by id", err);
// });
// }
}

View file

@ -17,8 +17,8 @@ export default abstract class CommunicationService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("communications not found by userid");
.catch((err) => {
throw new InternalException("communications not found by userid", err);
});
}

View file

@ -17,8 +17,8 @@ export default abstract class CommunicationTypeService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("communicationTypes not found");
.catch((err) => {
throw new InternalException("communicationTypes not found", err);
});
}
@ -35,8 +35,8 @@ export default abstract class CommunicationTypeService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("communicationType not found by id");
.catch((err) => {
throw new InternalException("communicationType not found by id", err);
});
}
@ -54,8 +54,8 @@ export default abstract class CommunicationTypeService {
// .then((res) => {
// return [];
// })
// .catch(() => {
// throw new InternalException("communicationType assigned members not found by id");
// .catch((err) => {
// throw new InternalException("communicationType assigned members not found by id", err);
// });
// }
}

View file

@ -16,8 +16,8 @@ export default abstract class ExecutivePositionService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("executivePositions not found");
.catch((err) => {
throw new InternalException("executivePositions not found", err);
});
}
@ -34,8 +34,8 @@ export default abstract class ExecutivePositionService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("executivePosition not found by id");
.catch((err) => {
throw new InternalException("executivePosition not found by id", err);
});
}

View file

@ -20,7 +20,7 @@ export default abstract class InviteService {
return res;
})
.catch((err) => {
throw new InternalException("invite not found by mail and token");
throw new InternalException("invite not found by mail and token", err);
});
}
}

View file

@ -16,8 +16,8 @@ export default abstract class MembershipStatusService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("membershipStatuss not found");
.catch((err) => {
throw new InternalException("membershipStatuss not found", err);
});
}
@ -34,8 +34,8 @@ export default abstract class MembershipStatusService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("membershipStatus not found by id");
.catch((err) => {
throw new InternalException("membershipStatus not found by id", err);
});
}
@ -53,8 +53,8 @@ export default abstract class MembershipStatusService {
// .then((res) => {
// return [];
// })
// .catch(() => {
// throw new InternalException("membershipStatus assigned members not found by id");
// .catch((err) => {
// throw new InternalException("membershipStatus assigned members not found by id", err);
// });
// }
}

View file

@ -16,8 +16,8 @@ export default abstract class QualificationService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("qualifications not found");
.catch((err) => {
throw new InternalException("qualifications not found", err);
});
}
@ -34,8 +34,8 @@ export default abstract class QualificationService {
.then((res) => {
return res;
})
.catch(() => {
throw new InternalException("qualification not found by id");
.catch((err) => {
throw new InternalException("qualification not found by id", err);
});
}
@ -53,8 +53,8 @@ export default abstract class QualificationService {
// .then((res) => {
// return [];
// })
// .catch(() => {
// throw new InternalException("qualification assigned members not found by id");
// .catch((err) => {
// throw new InternalException("qualification assigned members not found by id", err);
// });
// }
}

View file

@ -20,7 +20,7 @@ export default abstract class RefreshService {
return res;
})
.catch((err) => {
throw new InternalException("refresh not found");
throw new InternalException("refresh not found", err);
});
}
}

View file

@ -19,7 +19,7 @@ export default abstract class RolePermissionService {
return res;
})
.catch((err) => {
throw new InternalException("permissions not found by role");
throw new InternalException("permissions not found by role", err);
});
}
@ -38,7 +38,7 @@ export default abstract class RolePermissionService {
return res;
})
.catch((err) => {
throw new InternalException("permissions not found by roles");
throw new InternalException("permissions not found by roles", err);
});
}
}

View file

@ -17,7 +17,7 @@ export default abstract class RoleService {
return res;
})
.catch((err) => {
throw new InternalException("roles not found");
throw new InternalException("roles not found", err);
});
}
@ -37,7 +37,7 @@ export default abstract class RoleService {
return res;
})
.catch((err) => {
throw new InternalException("role not found by id");
throw new InternalException("role not found by id", err);
});
}
}

View file

@ -18,7 +18,7 @@ export default abstract class UserPermissionService {
return res;
})
.catch((err) => {
throw new InternalException("permission not found by user");
throw new InternalException("permission not found by user", err);
});
}
}

View file

@ -20,7 +20,7 @@ export default abstract class UserService {
return res;
})
.catch((err) => {
throw new InternalException("users not found");
throw new InternalException("users not found", err);
});
}
@ -42,7 +42,7 @@ export default abstract class UserService {
return res;
})
.catch((err) => {
throw new InternalException("user not found by id");
throw new InternalException("user not found by id", err);
});
}
@ -62,7 +62,7 @@ export default abstract class UserService {
return res;
})
.catch((err) => {
throw new InternalException("user not found by username");
throw new InternalException("user not found by username", err);
});
}
@ -84,7 +84,7 @@ export default abstract class UserService {
return res;
})
.catch((err) => {
throw new InternalException("user not found by mail or username");
throw new InternalException("user not found by mail or username", err);
});
}
@ -102,7 +102,7 @@ export default abstract class UserService {
return res;
})
.catch((err) => {
throw new InternalException("could not count users");
throw new InternalException("could not count users", err);
});
}
@ -123,7 +123,7 @@ export default abstract class UserService {
return res.roles;
})
.catch((err) => {
throw new InternalException("could not get roles for user");
throw new InternalException("could not get roles for user", err);
});
}
}