Skip to content

Commit

Permalink
error handling completed
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 15, 2024
1 parent 64e6c3e commit 55d87c6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
5 changes: 3 additions & 2 deletions server/src/infrastructure/repositories/PatientRepository.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down
7 changes: 4 additions & 3 deletions server/src/infrastructure/services/JWTService.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/use_case/doctor/AuthenticationUseCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 55d87c6

Please sign in to comment.