Fix API routing prefix and improve error reporting

This commit is contained in:
Automated Action 2025-05-16 06:09:14 +00:00
parent d48cd52fb7
commit 1280fe986a
2 changed files with 104 additions and 6 deletions

View File

@ -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

44
main.py
View File

@ -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()
}