fix(middlewares): add error catcher

This commit is contained in:
OxSaitama 2023-07-25 11:34:38 +02:00
parent 69585af4db
commit 3049dfcd75
13 changed files with 362 additions and 287 deletions

View File

@ -4,6 +4,7 @@ import { NextFunction, Request, Response } from "express";
import Container from "typedi"; import Container from "typedi";
export default function authHandler(req: Request, response: Response, next: NextFunction) { export default function authHandler(req: Request, response: Response, next: NextFunction) {
try {
const authHeader = req.headers["authorization"]; const authHeader = req.headers["authorization"];
const token = authHeader && authHeader.split(" ")[1]; const token = authHeader && authHeader.split(" ")[1];
@ -21,4 +22,10 @@ export default function authHandler(req: Request, response: Response, next: Next
req.body.user = userPayload; req.body.user = userPayload;
next(); next();
}); });
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -4,10 +4,11 @@ import { NextFunction, Request, Response } from "express";
import Container from "typedi"; import Container from "typedi";
export default async function documentHandler(req: Request, response: Response, next: NextFunction) { export default async function documentHandler(req: Request, response: Response, next: NextFunction) {
try {
const customerId = req.body.user.customerId; const customerId = req.body.user.customerId;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
if(!uid) { if (!uid) {
response.status(HttpCodes.BAD_REQUEST).send("Missing document uid"); response.status(HttpCodes.BAD_REQUEST).send("Missing document uid");
return; return;
} }
@ -15,9 +16,16 @@ export default async function documentHandler(req: Request, response: Response,
const documentService = Container.get(DocumentsService); const documentService = Container.get(DocumentsService);
const document = await documentService.getByUid(uid); const document = await documentService.getByUid(uid);
if(document?.depositor_uid != customerId) { if (document?.depositor_uid != customerId) {
response.status(HttpCodes.UNAUTHORIZED).send("Not authorized with this depositor"); response.status(HttpCodes.UNAUTHORIZED).send("Not authorized with this depositor");
return; return;
} }
next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -34,4 +34,6 @@ export default async function fileHandler(req: Request, response: Response, next
return; return;
} }
} }
next();
} }

View File

@ -6,6 +6,7 @@ import Container from "typedi";
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService"; import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
export default async function deedHandler(req: Request, response: Response, next: NextFunction) { export default async function deedHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const documentTypes: DocumentType[] = req.body.document_types; const documentTypes: DocumentType[] = req.body.document_types;
@ -41,4 +42,10 @@ export default async function deedHandler(req: Request, response: Response, next
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -6,6 +6,7 @@ import Container from "typedi";
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService"; import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
export default async function deedTypeHandler(req: Request, response: Response, next: NextFunction) { export default async function deedTypeHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const documentTypes: DocumentType[] = req.body.document_types; const documentTypes: DocumentType[] = req.body.document_types;
@ -47,4 +48,10 @@ export default async function deedTypeHandler(req: Request, response: Response,
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -8,6 +8,7 @@ import DocumentTypesService from "@Services/super-admin/DocumentTypesService/Doc
import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService"; import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/OfficeFoldersService";
export default async function documentHandler(req: Request, response: Response, next: NextFunction) { export default async function documentHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const documentType: DocumentType = req.body.document_type; const documentType: DocumentType = req.body.document_type;
@ -55,4 +56,10 @@ export default async function documentHandler(req: Request, response: Response,
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -4,6 +4,7 @@ import Container from "typedi";
import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService"; import DocumentTypesService from "@Services/super-admin/DocumentTypesService/DocumentTypesService";
export default async function documentTypeHandler(req: Request, response: Response, next: NextFunction) { export default async function documentTypeHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const office = req.body.office; const office = req.body.office;
@ -29,4 +30,10 @@ export default async function documentTypeHandler(req: Request, response: Respon
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -5,6 +5,7 @@ import FilesService from "@Services/common/FilesService/FilesService";
import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService"; import DocumentsService from "@Services/super-admin/DocumentsService/DocumentsService";
export default async function fileHandler(req: Request, response: Response, next: NextFunction) { export default async function fileHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
let uid = req.path && req.path.split("/")[5]; let uid = req.path && req.path.split("/")[5];
const document = req.body.document; const document = req.body.document;
@ -39,4 +40,9 @@ export default async function fileHandler(req: Request, response: Response, next
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -5,6 +5,7 @@ import OfficeFoldersService from "@Services/super-admin/OfficeFoldersService/Off
import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService"; import DeedTypesService from "@Services/super-admin/DeedTypesService/DeedTypesService";
export default async function folderHandler(req: Request, response: Response, next: NextFunction) { export default async function folderHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const office = req.body.office; const office = req.body.office;
@ -16,7 +17,7 @@ export default async function folderHandler(req: Request, response: Response, ne
return; return;
} }
if (deed) { if (deed && deed.deed_type) {
const deedTypeService = Container.get(DeedTypesService); const deedTypeService = Container.get(DeedTypesService);
const deedTypeWithOffice = await deedTypeService.getByUidWithOffice(deed.deed_type.uid!); const deedTypeWithOffice = await deedTypeService.getByUidWithOffice(deed.deed_type.uid!);
if (!deedTypeWithOffice) { if (!deedTypeWithOffice) {
@ -56,4 +57,10 @@ export default async function folderHandler(req: Request, response: Response, ne
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -4,6 +4,7 @@ import Container from "typedi";
import OfficeRolesService from "@Services/super-admin/OfficeRolesService/OfficeRolesService"; import OfficeRolesService from "@Services/super-admin/OfficeRolesService/OfficeRolesService";
export default async function officeRoleHandler(req: Request, response: Response, next: NextFunction) { export default async function officeRoleHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const office = req.body.office; const office = req.body.office;
@ -29,4 +30,10 @@ export default async function officeRoleHandler(req: Request, response: Response
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -4,6 +4,7 @@ import Container from "typedi";
import UsersService from "@Services/super-admin/UsersService/UsersService"; import UsersService from "@Services/super-admin/UsersService/UsersService";
export default async function userHandler(req: Request, response: Response, next: NextFunction) { export default async function userHandler(req: Request, response: Response, next: NextFunction) {
try {
const officeId = req.body.user.office_Id; const officeId = req.body.user.office_Id;
const uid = req.path && req.path.split("/")[5]; const uid = req.path && req.path.split("/")[5];
const office = req.body.office_membership; const office = req.body.office_membership;
@ -29,4 +30,9 @@ export default async function userHandler(req: Request, response: Response, next
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -2,10 +2,11 @@ import HttpCodes from "@Common/system/controller-pattern/HttpCodes";
import { NextFunction, Request, Response } from "express"; import { NextFunction, Request, Response } from "express";
export default async function ruleHandler(req: Request, response: Response, next: NextFunction) { export default async function ruleHandler(req: Request, response: Response, next: NextFunction) {
try {
const rules = req.body.user.rules; const rules = req.body.user.rules;
const service = req.path && req.path.split("/")[4]; const service = req.path && req.path.split("/")[4];
if(!rules){ if (!rules) {
response.status(HttpCodes.UNAUTHORIZED).send("Missing rules in JWT"); response.status(HttpCodes.UNAUTHORIZED).send("Missing rules in JWT");
return; return;
} }
@ -23,4 +24,10 @@ export default async function ruleHandler(req: Request, response: Response, next
} }
next(); next();
} catch (error) {
console.log(error);
response.status(HttpCodes.INTERNAL_ERROR).send("Internal server error");
return;
}
} }

View File

@ -88,7 +88,6 @@ export class BackendVariables {
this.ENV = process.env["ENV"]!; this.ENV = process.env["ENV"]!;
} }
public async validate(groups?: string[]) { public async validate(groups?: string[]) {
console.log(this);
const validationOptions = groups ? { groups } : undefined; const validationOptions = groups ? { groups } : undefined;
@ -98,8 +97,6 @@ export class BackendVariables {
if (process.env["ENV"] === "dev") { if (process.env["ENV"] === "dev") {
throw error; throw error;
} }
console.error(error);
console.error(this);
throw new Error("Some env variables are required!"); throw new Error("Some env variables are required!");
} }
return this; return this;