Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
chore: added srcFun name to all the custom Error classes.
  • Loading branch information
hanoak20 committed Jan 29, 2024
commit d2f28159ede88d6c479d8bb9e31febe7dfc8a326
1 change: 1 addition & 0 deletions src/middlewares/error.middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const errorMiddleware = (
logger.error(err.stack);

if (err instanceof AppError) {
logger.error(`Error in method: ${err.srcFunc}`);
res
.status(err.statusCode)
.json({ error: { code: err.statusCode, message: err.message } });
Expand Down
7 changes: 5 additions & 2 deletions src/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const login = async (req: Request): Promise<LoginServiceType> => {
};

if (!res?.data?.user)
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER);
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER, "login");

const appTokenPayload: AppTokenPayload = {
region: userData?.region,
Expand Down Expand Up @@ -106,7 +106,10 @@ const requestSms = async (req: Request): Promise<LoginServiceType> => {
status: res.status,
};
} catch (error) {
throw new InternalServerError();
throw new InternalServerError(
constants.HTTP_TEXTS.INTERNAL_ERROR,
"requestSms"
);
}
};

Expand Down
23 changes: 16 additions & 7 deletions src/services/projects.migrations.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import { MigrationQueryType } from "../models/types";
const _getProject = async (projectId: string, query: MigrationQueryType) => {
if (!isValidObjectId(projectId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "project")
constants.HTTP_TEXTS.INVALID_ID.replace("$", "project"),
"_getProject"
);

const project = await ProjectModel.findOne(query);

if (!project) throw new NotFoundError(constants.HTTP_TEXTS.NO_PROJECT);
if (!project)
throw new NotFoundError(constants.HTTP_TEXTS.NO_PROJECT, "_getProject");

return project;
};
Expand Down Expand Up @@ -68,7 +70,10 @@ const createMigration = async (req: Request) => {
});

if (project.migration?.length)
throw new BadRequestError(constants.HTTP_TEXTS.MIGRATION_EXISTS);
throw new BadRequestError(
constants.HTTP_TEXTS.MIGRATION_EXISTS,
"createMigration"
);

project.migration.push({
_id: getMongooseID(),
Expand Down Expand Up @@ -109,7 +114,8 @@ const updateMigration = async (req: Request) => {

if (!isValidObjectId(migrationId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration")
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration"),
"updateMigration"
);

const project = await _getProject(projectId, {
Expand Down Expand Up @@ -143,7 +149,8 @@ const updateMigrationLegacyCMS = async (req: Request) => {

if (!isValidObjectId(migrationId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration")
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration"),
"updateMigrationLegacyCMS"
);

const project = await _getProject(projectId, {
Expand Down Expand Up @@ -172,7 +179,8 @@ const updateMigrationFileFormat = async (req: Request) => {

if (!isValidObjectId(migrationId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration")
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration"),
"updateMigrationFileFormat"
);

const project = await _getProject(projectId, {
Expand Down Expand Up @@ -203,7 +211,8 @@ const deleteMigration = async (req: Request) => {

if (!isValidObjectId(migrationId))
throw new BadRequestError(
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration")
constants.HTTP_TEXTS.INVALID_ID.replace("$", "migration"),
"deleteMigration"
);

const filter = {
Expand Down
10 changes: 8 additions & 2 deletions src/services/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const getUserProfile = async (req: Request): Promise<UserProfile> => {
}).lean();

if (!user?.authtoken)
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER);
throw new BadRequestError(
constants.HTTP_TEXTS.NO_CS_USER,
"getUserProfile"
);

const res = await https({
method: "GET",
Expand All @@ -29,7 +32,10 @@ const getUserProfile = async (req: Request): Promise<UserProfile> => {
});

if (!res?.data?.user)
throw new BadRequestError(constants.HTTP_TEXTS.NO_CS_USER);
throw new BadRequestError(
constants.HTTP_TEXTS.NO_CS_USER,
"getUserProfile"
);

const orgs = (res?.data?.user?.organizations || [])
?.filter((org: any) => org?.org_roles?.some((item: any) => item.admin))
Expand Down
33 changes: 21 additions & 12 deletions src/utils/custom-errors.utils.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
import { constants } from "../constants";

export class AppError extends Error {
constructor(public statusCode: number, message: string) {
srcFunc: string;
constructor(
public statusCode: number,
message: string,
srcFunc: string = ""
) {
super(message);
this.srcFunc = srcFunc;
Object.setPrototypeOf(this, AppError.prototype);
}
}

export class NotFoundError extends AppError {
constructor(message: string = "Not Found") {
super(constants.HTTP_CODES.NOT_FOUND, message);
constructor(message: string = "Not Found", srcFunc: string = "") {
super(constants.HTTP_CODES.NOT_FOUND, message, srcFunc);
}
}

export class BadRequestError extends AppError {
constructor(message: string = "Bad Request") {
super(constants.HTTP_CODES.BAD_REQUEST, message);
constructor(message: string = "Bad Request", srcFunc: string = "") {
super(constants.HTTP_CODES.BAD_REQUEST, message, srcFunc);
}
}

export class DatabaseError extends AppError {
constructor(message: string = "DB error") {
super(constants.HTTP_CODES.SERVER_ERROR, message);
constructor(message: string = "DB error", srcFunc: string = "") {
super(constants.HTTP_CODES.SERVER_ERROR, message, srcFunc);
}
}

export class ValidationError extends AppError {
constructor(message: string = "User validation error") {
super(constants.HTTP_CODES.UNPROCESSABLE_CONTENT, message);
constructor(message: string = "User validation error", srcFunc: string = "") {
super(constants.HTTP_CODES.UNPROCESSABLE_CONTENT, message, srcFunc);
}
}

export class InternalServerError extends AppError {
constructor(message: string = constants.HTTP_TEXTS.INTERNAL_ERROR) {
super(constants.HTTP_CODES.SERVER_ERROR, message);
constructor(
message: string = constants.HTTP_TEXTS.INTERNAL_ERROR,
srcFunc: string = ""
) {
super(constants.HTTP_CODES.SERVER_ERROR, message, srcFunc);
}
}

export class UnauthorizedError extends AppError {
constructor(message: string = constants.HTTP_TEXTS.UNAUTHORIZED) {
super(constants.HTTP_CODES.UNAUTHORIZED, message);
super(constants.HTTP_CODES.UNAUTHORIZED, message, "Auth utils");
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/validators/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default (route: string = "") =>
.map((field) => field.array())
.reduce((acc, val) => [...acc, ...val], []);

if (result.length) throw new ValidationError(result[0].msg);
if (result.length) throw new ValidationError(result[0].msg, "validation");

return next();
});