From 1280fe986a6e5c93b4e55eea6df27cfbef589cf0 Mon Sep 17 00:00:00 2001 From: Automated Action Date: Fri, 16 May 2025 06:09:14 +0000 Subject: [PATCH] Fix API routing prefix and improve error reporting --- README.md | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 44 ++++++++++++++++++++++++++++++++----- 2 files changed, 104 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a248b82..872357d 100644 --- a/README.md +++ b/README.md @@ -33,6 +33,72 @@ A RESTful API for managing tasks, built with FastAPI and SQLite. ### Health Check - `GET /health`: Application health check +- `GET /db-test`: Database connection diagnostic endpoint + +## Example Curl Commands + +### Create a new task + +```bash +curl -X 'POST' \ + 'https://your-domain.com/api/v1/tasks/' \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{ + "title": "Complete project documentation", + "description": "Write comprehensive documentation for the project", + "priority": "high", + "status": "todo", + "due_date": "2023-06-30T18:00:00.000Z", + "completed": false +}' +``` + +### Get all tasks + +```bash +curl -X 'GET' \ + 'https://your-domain.com/api/v1/tasks/' \ + -H 'accept: application/json' +``` + +### Get a specific task + +```bash +curl -X 'GET' \ + 'https://your-domain.com/api/v1/tasks/1' \ + -H 'accept: application/json' +``` + +### Update a task + +```bash +curl -X 'PUT' \ + 'https://your-domain.com/api/v1/tasks/1' \ + -H 'accept: application/json' \ + -H 'Content-Type: application/json' \ + -d '{ + "title": "Updated title", + "description": "Updated description", + "status": "in_progress" +}' +``` + +### Delete a task + +```bash +curl -X 'DELETE' \ + 'https://your-domain.com/api/v1/tasks/1' \ + -H 'accept: application/json' +``` + +### Mark a task as completed + +```bash +curl -X 'POST' \ + 'https://your-domain.com/api/v1/tasks/1/complete' \ + -H 'accept: application/json' +``` ## Project Structure diff --git a/main.py b/main.py index f4bf772..22711a8 100644 --- a/main.py +++ b/main.py @@ -35,12 +35,19 @@ app.add_middleware( # Add exception handlers for better error reporting @app.exception_handler(Exception) async def validation_exception_handler(request: Request, exc: Exception): + import traceback + error_detail = { + "detail": f"Internal Server Error: {str(exc)}", + "type": str(type(exc).__name__), + "traceback": traceback.format_exc().split("\n") + } + print(f"Error processing request: {error_detail}") return JSONResponse( status_code=500, - content={"detail": f"Internal Server Error: {str(exc)}"}, + content=error_detail, ) -app.include_router(api_router) +app.include_router(api_router, prefix=settings.API_V1_STR) @app.get("/health", tags=["health"]) @@ -56,21 +63,46 @@ def test_db_connection(): """ Test database connection and table creation """ - from sqlalchemy import text + from sqlalchemy import text, inspect from app.db.session import engine + import traceback try: + inspector = inspect(engine) + tables = inspector.get_table_names() + + table_info = {} + for table in tables: + columns = inspector.get_columns(table) + table_info[table] = [col['name'] for col in columns] + with engine.connect() as conn: # Try to select from the task table to verify it exists - result = conn.execute(text("SELECT COUNT(*) FROM task")).scalar() + if 'task' in tables: + result = conn.execute(text("SELECT COUNT(*) FROM task")).scalar() + task_count = result + else: + task_count = "Table 'task' not found" + + # Check DB directory + import os + from app.core.config import DB_DIR + db_path = f"{DB_DIR}/db.sqlite" + db_exists = os.path.exists(db_path) + return { "status": "ok", "connection": "successful", - "task_count": result + "tables": tables, + "table_details": table_info, + "task_count": task_count, + "db_path": db_path, + "db_exists": db_exists } except Exception as e: return { "status": "error", "connection": "failed", - "error": str(e) + "error": str(e), + "traceback": traceback.format_exc() } \ No newline at end of file