From 55d87c65dbc3102ea1a7feb42111018012b71ad6 Mon Sep 17 00:00:00 2001 From: Sinan Date: Sun, 15 Sep 2024 15:33:28 +0530 Subject: [PATCH] error handling completed --- .../src/infrastructure/repositories/PatientRepository.ts | 5 +++-- server/src/infrastructure/services/JWTService.ts | 7 ++++--- server/src/use_case/doctor/AuthenticationUseCase.ts | 2 +- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/server/src/infrastructure/repositories/PatientRepository.ts b/server/src/infrastructure/repositories/PatientRepository.ts index f0672bfb..ddd046f1 100644 --- a/server/src/infrastructure/repositories/PatientRepository.ts +++ b/server/src/infrastructure/repositories/PatientRepository.ts @@ -1,6 +1,7 @@ +import CustomError from "../../domain/entities/CustomError"; import { IPatient } from "../../domain/entities/IPatient"; import IPatientRepository from "../../domain/interface/repositories/IPatientRepository"; -import { PaginatedResult } from "../../types"; +import { PaginatedResult, StatusCode } from "../../types"; import PatientModel from "../database/PatientModel"; export default class PatientRepository implements IPatientRepository { @@ -30,7 +31,7 @@ export default class PatientRepository implements IPatientRepository { return await patientModel.save(); } catch (error: any) { if (error.code === 11000) { - throw new Error("Email Already Exists"); + throw new CustomError("Email Already Exists", StatusCode.Conflict); } throw error; } diff --git a/server/src/infrastructure/services/JWTService.ts b/server/src/infrastructure/services/JWTService.ts index 21b8fd09..33517e6c 100644 --- a/server/src/infrastructure/services/JWTService.ts +++ b/server/src/infrastructure/services/JWTService.ts @@ -1,6 +1,7 @@ import ITokenService from "../../domain/interface/services/ITokenService"; import jwt, { JwtPayload, TokenExpiredError } from "jsonwebtoken"; -import { UserRole } from "../../types/index"; +import { StatusCode, UserRole } from "../../types/index"; +import CustomError from "../../domain/entities/CustomError"; export default class JWTService implements ITokenService { private signToken(payload: object, secret: string, expiresIn: string): string { @@ -11,9 +12,9 @@ export default class JWTService implements ITokenService { return jwt.verify(token, secret) as JwtPayload; } catch (error) { if (error instanceof TokenExpiredError) { - throw new Error("Token Expired"); + throw new CustomError("Token Expired",StatusCode.Unauthorized); } - throw new Error("Invalid token"); + throw new CustomError("Invalid token",StatusCode.Forbidden); } } diff --git a/server/src/use_case/doctor/AuthenticationUseCase.ts b/server/src/use_case/doctor/AuthenticationUseCase.ts index 9d29e46d..d6b51a80 100644 --- a/server/src/use_case/doctor/AuthenticationUseCase.ts +++ b/server/src/use_case/doctor/AuthenticationUseCase.ts @@ -28,7 +28,7 @@ export default class AuthenticationUseCase { if (doctor.isBlocked) throw new CustomError("Doctor is Blocked", StatusCode.Forbidden); if (doctor.role !== "doctor") throw new CustomError("Invalid Credentials", StatusCode.Unauthorized); if (!(await this.passwordService.compare(password, doctor.password!))) throw new CustomError("Invalid Credentials", StatusCode.Unauthorized); - if (!doctor.isVerified) throw new Error("Not Verified"); + if (!doctor.isVerified) throw new CustomError("Not Verified",StatusCode.Unauthorized); let otp = +this.generateOTP(6); while (otp.toString().length !== 6) {