-
Notifications
You must be signed in to change notification settings - Fork 0
Partially Addressed Error Handling for API Route #17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
|
|
||
| All routes are prefixed with /api/v1/inventory | ||
| """ | ||
| from fastapi import APIRouter | ||
| from fastapi import APIRouter, HTTPException | ||
| from models.snack import ( | ||
| Snack, | ||
| SnackCreateSchema, | ||
|
|
@@ -24,26 +24,102 @@ | |
| update_snack | ||
| ) | ||
|
|
||
| def raise_internal_error(): | ||
| raise HTTPException(status_code=500, detail= | ||
| { | ||
| "error": { | ||
| "code": "INTERNAL_SERVER_ERROR", | ||
| "message": "An internal error occured", | ||
| } | ||
| }) | ||
| def raise_not_found_error(): | ||
| raise HTTPException(status_code=404, detail= | ||
| { | ||
| "error": { | ||
| "code": "NOT_FOUND", | ||
| "message": "Resource not found", | ||
| } | ||
| }) | ||
| def raise_bad_request_error(): | ||
| raise HTTPException(status_code=400, detail= | ||
| { | ||
| "error": { | ||
| "code": "INVALID INPUT", | ||
| "message": "The Response Model or SKU is invalid", | ||
| } | ||
| }) | ||
| router = APIRouter() | ||
|
|
||
| @router.get("/", response_model=InventoryResponse) | ||
| async def get_inventory_route(): | ||
| snacks = get_inventory() | ||
| return { "snacks": snacks } | ||
| try: | ||
| snacks = get_inventory() | ||
| if not snacks: | ||
| raise_not_found_error() | ||
| return { "snacks": snacks } | ||
| except Exception as e: | ||
| raise_internal_error() | ||
|
|
||
|
|
||
| @router.get("/snacks/{sku}", response_model=Snack) | ||
| async def get_snack_route(sku: str): | ||
| snack = get_snack(sku) | ||
| return snack | ||
| #check duplicate entry (i dont think this even needs to be here LOL) | ||
| try: | ||
| #check input validation (UPDATE THIS WHEN WE HAVE VALIDATION) | ||
| if not sku: | ||
| raise_bad_request_error() | ||
| snack = get_snack(sku) | ||
| #check if snack exists | ||
| if not snack: | ||
| raise_not_found_error() | ||
| return snack | ||
| #check for internal server error | ||
| except Exception as e: | ||
| raise_internal_error() | ||
|
Comment on lines
+77
to
+78
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?) |
||
|
|
||
| @router.post("/snacks", response_model=Snack) | ||
| async def create_snack_route(snack: SnackCreateSchema): | ||
| return create_snack(snack) | ||
| #check input validation (UPDATE THIS WHEN WE HAVE VALIDATION) | ||
| #check duplicate entry (no method yet) | ||
| try: | ||
| snack = create_snack(snack) | ||
| #check if snack exists | ||
| if not snack: | ||
| raise_bad_request_error() | ||
| return snack | ||
| #check for internal server error | ||
| except Exception as e: | ||
| raise_internal_error() | ||
|
Comment on lines
+91
to
+92
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?) |
||
|
|
||
| @router.put("/snacks/{sku}", response_model=Snack) | ||
| async def update_snack_route(sku: str, updates: SnackUpdateSchema): | ||
| return update_snack(sku, updates) | ||
| #check duplicate entry (no method yet) | ||
| try: | ||
| #check input validation (UPDATE THIS WHEN WE HAVE VALIDATION) | ||
| if not sku: | ||
| raise_bad_request_error() | ||
|
|
||
| updated = update_snack(sku, updates) | ||
| #check if snack exists | ||
| if not updated: | ||
| raise_not_found_error() | ||
| return updated | ||
| #check for internal server error | ||
| except Exception as e: | ||
| raise_internal_error() | ||
|
Comment on lines
+108
to
+109
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?) |
||
|
|
||
| @router.delete("/snacks/{sku}", response_model=Snack) | ||
| async def delete_snack_route(sku: str): | ||
| return delete_snack(sku) | ||
| #check duplicate entry (no method yet) | ||
| try: | ||
| #check input validation (UPDATE THIS WHEN WE HAVE VALIDATION) | ||
| if not sku: | ||
| raise_bad_request_error() | ||
| snack = delete_snack(sku) | ||
| #check if snack exists | ||
| if not snack: | ||
| raise_not_found_error() | ||
| return snack | ||
| #check for internal server error | ||
| except Exception as e: | ||
| raise_internal_error() | ||
|
Comment on lines
+124
to
+125
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is catching an exception then raising another exception which isn't caught. perhaps we can change this so that the exception is caught. (maybe we raise it in the try block instead of the catch block?) |
||
Uh oh!
There was an error while loading. Please reload this page.