feat: custom exceptions instead of direct fastapi.httpexceptions
Resolves #2
This commit is contained in:
parent
d3d3b2ca63
commit
868e56ce40
9 changed files with 73 additions and 43 deletions
|
|
@ -14,10 +14,12 @@ Endpoints:
|
|||
"""
|
||||
from typing import Annotated, Optional
|
||||
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from fastapi import APIRouter, status
|
||||
from fastapi.params import Query
|
||||
|
||||
from src.exceptions import UnprocessableContent
|
||||
from src.contact.schemas import ContactAddress
|
||||
from src.contact.exceptions import ContactNotFoundException
|
||||
from src.database import db_dependency
|
||||
from src.contact.models import Contact
|
||||
from src.user.models import User
|
||||
|
|
@ -150,7 +152,7 @@ async def remove_user_from_org(db: db_dependency, org_model: org_model_body_depe
|
|||
raise UserNotFoundException(user_id=user_id)
|
||||
|
||||
if user not in org_model.user_rel:
|
||||
raise HTTPException(status_code=status.HTTP_204_NOT_FOUND)
|
||||
return
|
||||
|
||||
org_model.user_rel.remove(user)
|
||||
db.commit()
|
||||
|
|
@ -166,10 +168,10 @@ async def get_contact(org_model: org_model_query_dependency, contact_type: Annot
|
|||
case "owner":
|
||||
contact_model = org_model.owner_contact_rel
|
||||
case _:
|
||||
raise HTTPException(status_code=422, detail="Invalid contact type")
|
||||
raise UnprocessableContent("Invalid contact type")
|
||||
|
||||
if contact_model is None:
|
||||
raise HTTPException(status_code=404, detail="Contact not found")
|
||||
raise ContactNotFoundException()
|
||||
|
||||
return OrgContactGetResponse.model_construct(
|
||||
**contact_model.__dict__,
|
||||
|
|
@ -187,17 +189,17 @@ async def update_contact(db: db_dependency, org_model: org_model_body_dependency
|
|||
case "owner":
|
||||
contact_model = org_model.owner_contact_rel
|
||||
case _:
|
||||
raise HTTPException(status_code=422, detail="Invalid contact type")
|
||||
raise UnprocessableContent("Invalid contact type")
|
||||
|
||||
if contact_model is None:
|
||||
raise HTTPException(status_code=404, detail="Contact not found")
|
||||
raise ContactNotFoundException()
|
||||
|
||||
update_data = request_model.model_dump(exclude_none=True)
|
||||
for key, value in update_data.items():
|
||||
if hasattr(contact_model, key):
|
||||
setattr(contact_model, key, value)
|
||||
else:
|
||||
raise HTTPException(status_code=422, detail="Invalid keys in update request")
|
||||
raise UnprocessableContent("Invalid keys in update request")
|
||||
db.flush()
|
||||
|
||||
response = OrgContactGetResponse.model_construct(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue