From cfb1321e1e6a23d4913a40674ac9e9a31c288cc5 Mon Sep 17 00:00:00 2001 From: coolneng Date: Fri, 24 Jul 2020 19:27:15 +0200 Subject: [PATCH] Simplify error handling in routes --- app/__init__.py | 2 +- app/routes.py | 11 +++++------ tests/queries_test.py | 1 + 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/app/__init__.py b/app/__init__.py index 354ce4c..a358d25 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -2,4 +2,4 @@ from fastapi import FastAPI app = FastAPI() -from app import routes, schema +from app import routes, schemas diff --git a/app/routes.py b/app/routes.py index 57b21ec..79ae2e5 100644 --- a/app/routes.py +++ b/app/routes.py @@ -1,4 +1,4 @@ -from fastapi import Response, status +from fastapi import HTTPException, Response, status from app import app from app.schemas import * @@ -6,7 +6,7 @@ from app.twilio import send_otp from database.crud import insert_data, verify_otp -@app.post("/register", status_code=status.HTTP_200_OK) +@app.post("/register") async def create_user(request: RegisterSchema): insert_data(schema="Users", data=request) send_otp(receiver=request.mobile) @@ -14,16 +14,15 @@ async def create_user(request: RegisterSchema): # FIXME Use OAuth2 for verification -@app.post("/login", status_code=status.HTTP_200_OK) +@app.post("/login") async def log_in(request: LoginSchema, response: Response): return {"message": "Logged in successfully"} # response.status_code = status.HTTP_400_BAD_REQUEST # return {"message": "The email/password combination is not correct"} -@app.post("/otpVerification", status_code=status.HTTP_200_OK) +@app.post("/otpVerification") async def validate_otp(request: OTPSchema, response: Response): if verify_otp(data=request): return {"message": "The OTP has been verified successfully"} - response.status_code = status.HTTP_400_BAD_REQUEST - return {"message": "The OTP is not correct"} + raise HTTPException(status_code=400, detail="The OTP is not correct") diff --git a/tests/queries_test.py b/tests/queries_test.py index 309c9ba..5def271 100644 --- a/tests/queries_test.py +++ b/tests/queries_test.py @@ -3,6 +3,7 @@ from datetime import datetime from database import SessionLocal from database.models import * + db = SessionLocal()