Skip to content

API Route Error Handling #3

@NicholasLe04

Description

@NicholasLe04

Add Error Handling to Inventory Router

Overview

The inventory router (backend/routers/v1/inventory.py) currently lacks comprehensive error handling. We need to implement proper error handling to ensure our API returns appropriate error responses when operations fail.

Current State

The router handles basic CRUD operations for inventory management but doesn't handle edge cases and potential errors such as:

  • Non-existent SKUs
  • Invalid data formats
  • Database operation failures
  • Duplicate SKU creation attempts

Objectives

  1. Implement try-catch blocks for all database operations
  2. Add proper HTTP status codes for error scenarios
  3. Create standardized error response formats
  4. Add input validation where necessary

Detailed Tasks

1. Error Handling Structure

Add error handling for these specific endpoints:

  • GET /api/v1/inventory
  • GET /api/v1/inventory/snacks/{sku}
  • POST /api/v1/inventory/snacks
  • PUT /api/v1/inventory/snacks/{sku}
  • DELETE /api/v1/inventory/snacks/{sku}

2. Implement These Error Scenarios

For each endpoint, handle these cases:

  • 404 Not Found: When SKU doesn't exist
  • 400 Bad Request: When input validation fails
  • 409 Conflict: When trying to create a duplicate SKU
  • 500 Internal Server Error: For database operation failures

3. Create Error Response Format

Implement a consistent error response structure:

{
    "error": {
        "code": "ERROR_CODE",
        "message": "Human readable message",
        "details": {} # Optional additional information
    }
}

Example Implementation

Here's a starting point for one endpoint:

from fastapi import HTTPException

@router.get("/snacks/{sku}", response_model=Snack)
async def get_snack_route(sku: str):
    try:
        snack = get_snack(sku)
        if not snack:
            raise HTTPException(
                status_code=404,
                detail={
                    "error": {
                        "code": "SNACK_NOT_FOUND",
                        "message": f"Snack with SKU {sku} not found"
                    }
                }
            )
        return snack
    except Exception as e:
        raise HTTPException(
            status_code=500,
            detail={
                "error": {
                    "code": "INTERNAL_ERROR",
                    "message": "An internal error occurred"
                }
            }
        )

Testing Requirements

  • Write test cases for each error scenario
  • Verify correct status codes are returned
  • Verify error response format is consistent
  • Test with invalid inputs

Resources

Definition of Done

  • Error handling implemented for all endpoints
  • Consistent error response format across all endpoints

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions