diff --git a/app/daos/home.py b/app/daos/home.py index a9d34da..89bad05 100644 --- a/app/daos/home.py +++ b/app/daos/home.py @@ -13,6 +13,6 @@ async def external_service_call(): # Simulate occasional failures if random.random() < 0.2: # 20% chance of failure #NOSONAR - raise ExternalServiceException + raise ExternalServiceException("External Service Failed") return "Success from external service" diff --git a/app/daos/users.py b/app/daos/users.py index 568da2b..5cfec73 100644 --- a/app/daos/users.py +++ b/app/daos/users.py @@ -26,7 +26,7 @@ async def get_user(user_id: int, db_session: Session): try: cache_key = f"user_{user_id}" - cached_user, expire = await retrieve_cache(cache_key) + cached_user, _ = await retrieve_cache(cache_key) if cached_user: return json.loads(cached_user) # Check if the user already exists in the database @@ -45,7 +45,7 @@ async def get_user(user_id: int, db_session: Session): .first() ) if not user: - raise NoUserFoundException + raise NoUserFoundException(messages['NO_USER_FOUND_FOR_ID']) await create_cache(json.dumps(user._asdict(), default=str), cache_key, 60) return user @@ -76,7 +76,7 @@ def create_user(data: CreateUser, db_session: Session): # Check if the email already exists in the db email_exists = check_existing_field(db_session=db_session, model=User, field="email", value=user_data["email"]) if email_exists: - raise EmailAlreadyExistException + raise EmailAlreadyExistException(messages['EMAIL_ALREADY_EXIST']) # Check if the mobile already exists in the db mobile_exists = check_existing_field( @@ -86,7 +86,7 @@ def create_user(data: CreateUser, db_session: Session): value=user_data["mobile"], ) if mobile_exists: - raise MobileAlreadyExistException + raise MobileAlreadyExistException(messages['MOBILE_ALREADY_EXIST']) user = User(**user_data) @@ -115,10 +115,10 @@ def login(data: Login, db_session: Session): ) if not user_details: - raise InvalidCredentialsException + raise InvalidCredentialsException(messages['INVALID_CREDENTIALS']) if not check_password_hash(user_details.password, user_data["password"]): - raise InvalidCredentialsException + raise InvalidCredentialsException(messages['INVALID_CREDENTIALS']) del user_details.password token = jwt_utils.create_access_token({"sub": user_details.email, "id": user_details.id}) diff --git a/app/exceptions.py b/app/exceptions.py index e95b0b1..984528f 100644 --- a/app/exceptions.py +++ b/app/exceptions.py @@ -1,49 +1,24 @@ -from __future__ import annotations - - class ExternalServiceException(Exception): - def __init__(self, message="External service failed"): - self.message = message - super().__init__(self.message) + pass class NoUserFoundException(Exception): - def __init__(self, message="No User found"): - self.message = message - super().__init__(self.message) - - + pass class EmailAlreadyExistException(Exception): - def __init__(self, message="Email already exists"): - self.message = message - super().__init__(self.message) - + pass class MobileAlreadyExistException(Exception): - def __init__(self, message="Mobile already exists"): - self.message = message - super().__init__(self.message) - + pass class InvalidCredentialsException(Exception): - def __init__(self, message="Invalid credentials"): - self.message = message - super().__init__(self.message) - - + pass + class CentryTestException(Exception): - def __init__(self, message="Centry Test"): - self.message = message - super().__init__(self.message) - + pass class DatabaseConnectionException(Exception): - def __init__(self, message="Database Connection Error"): - self.message = message - super().__init__(self.message) + pass class RedisUrlNotFoundException(Exception): - def __init__(self, message="Redis Url Not Found"): - self.message = message - super().__init__(self.message) + pass \ No newline at end of file diff --git a/app/middlewares/rate_limiter_middleware.py b/app/middlewares/rate_limiter_middleware.py index f879fbc..e9c1085 100644 --- a/app/middlewares/rate_limiter_middleware.py +++ b/app/middlewares/rate_limiter_middleware.py @@ -26,7 +26,7 @@ async def dispatch(self, request: Request, call_next): pipe.expire(client_ip, TIME_WINDOW) await pipe.execute() finally: - call_next(request) + print("Finally Block in Rate Limit exceeded") response = await call_next(request) return response diff --git a/app/routes/home/home.py b/app/routes/home/home.py index 36ae887..5dc4cbc 100644 --- a/app/routes/home/home.py +++ b/app/routes/home/home.py @@ -37,7 +37,7 @@ async def external_service_endpoint(): def sentry_endpoint(): if not settings.SENTRY_DSN: raise HTTPException(status_code=503, detail="Sentry DSN not found") - raise CentryTestException + raise CentryTestException("Centry Test") @home_router.get("/{path:path}", include_in_schema=False) diff --git a/app/sessions/db.py b/app/sessions/db.py index 3405252..e9ca8b4 100644 --- a/app/sessions/db.py +++ b/app/sessions/db.py @@ -63,7 +63,7 @@ print("\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx") except Exception as e: print(f"Failed to connect to database. Error: {e}") - raise DatabaseConnectionException + raise DatabaseConnectionException("Failed to connect to database") localSession = Session(engine) diff --git a/app/utils/exception_handler.py b/app/utils/exception_handler.py index a0509f4..6e1062a 100644 --- a/app/utils/exception_handler.py +++ b/app/utils/exception_handler.py @@ -19,7 +19,8 @@ async def validation_exception_handler(exc: RequestValidationError): ) -async def http_exception_handler(exc: HTTPException): +async def http_exception_handler(request:Request, exc: HTTPException): + print(request) return JSONResponse(status_code=exc.status_code, content={"success": False, "message": exc.detail}) diff --git a/app/wrappers/cache_wrappers.py b/app/wrappers/cache_wrappers.py index 3575c22..b96ec16 100644 --- a/app/wrappers/cache_wrappers.py +++ b/app/wrappers/cache_wrappers.py @@ -5,7 +5,7 @@ from app.exceptions import RedisUrlNotFoundException if not settings.REDIS_URL: - raise RedisUrlNotFoundException + raise RedisUrlNotFoundException("Failed To get Redis URL") async def create_cache(resp, key: str, ex: int = 60):