Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions nexios_contrib/tortoise/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,37 @@ def handle_tortoise_exceptions(app: "NexiosApp") -> None:
@app.add_exception_handler(IntegrityError)
async def handle_integrity(request: Request, response: Response, exc: IntegrityError):
logger.warning(f"Tortoise IntegrityError: {exc}")
return response.status(400).json({
return response.json({
"error": "Integrity constraint violation",
"detail": str(exc),
"type": "integrity_error"
})
}).status(400)

@app.add_exception_handler(DoesNotExist)
async def handle_not_found(request: Request, response: Response, exc: DoesNotExist):
logger.info(f"Tortoise DoesNotExist: {exc}")
return response.status(404).json({
return response.json({
"error": "Record not found",
"detail": str(exc),
"type": "not_found_error"
})
}).status(404)

@app.add_exception_handler(ValidationError)
async def handle_validation(request: Request, response: Response, exc: ValidationError):
logger.warning(f"Tortoise ValidationError: {exc}")
return response.status(422).json({
return response.json({
"error": "Validation failed",
"detail": str(exc),
"type": "validation_error"
})
}).status(422)

@app.add_exception_handler(OperationalError)
async def handle_operational(request: Request, response: Response, exc: OperationalError):
logger.error(f"Tortoise OperationalError: {exc}")
return response.status(503).json({
return response.json({
"error": "Database operational error",
"detail": "Service temporarily unavailable",
"type": "operational_error"
})
}).status(503)

logger.info("Tortoise exception handlers (4 types) registered.")
14 changes: 7 additions & 7 deletions tests/tortoise/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ async def list_users(request, response):
async def create_user(request, response):
data = await request.json
user = await User.create(**data)
return response.status(201).json({
return response.json({
"id": user.id,
"name": user.name,
"email": user.email,
"age": user.age,
"is_active": user.is_active
})
}).status(201)

@app.get("/users/{user_id}")
async def get_user(request, response):
Expand Down Expand Up @@ -138,7 +138,7 @@ async def delete_user(request, response):
try:
user = await User.get(id=user_id)
await user.delete()
return response.status(204)
return response.empty().status(204)
except User.DoesNotExist:
raise # Will be handled by exception handler

Expand Down Expand Up @@ -195,7 +195,7 @@ def test_foreign_key_relationships(self, app, client):
async def create_user(request, response):
data = await request.json
user = await User.create(**data)
return response.status(201).json({"id": user.id, "name": user.name})
return response.json({"id": user.id, "name": user.name}).status(201)

@app.post("/posts")
async def create_post(request, response):
Expand All @@ -207,11 +207,11 @@ async def create_post(request, response):
author=author,
published=data.get("published", False)
)
return response.status(201).json({
return response.json({
"id": post.id,
"title": post.title,
"author_id": author.id
})
}).status(201)

@app.get("/posts/{post_id}")
async def get_post_with_author(request, response):
Expand Down Expand Up @@ -262,7 +262,7 @@ def test_exception_handling_integration(self, app, client):
async def create_user(request, response):
data = await request.json
user = await User.create(**data)
return response.status(201).json({"id": user.id})
return response.json({"id": user.id}).status(201)

@app.get("/users/{user_id}")
async def get_user(request, response):
Expand Down
Loading