datawhalechina/hello-agents · error · HTTPException
str(e)
Error message
str(e)
What it means
Catch-all HTTPException 500 in the create-user endpoint (api/routes/users.py:52). Despite the '创建用户失败' log label, the except wraps user creation plus UserResponse construction; failures include DB write errors from db_manager and KeyError('id'/'email'/'profile') when get_user returns None because the just-created user could not be read back.
Source
Thrown at Co-creation-projects/Apricity-InnocoreAI/api/routes/users.py:52
"""创建用户"""
try:
user_id = await db_manager.create_user(
email=request.email,
profile=request.profile
)
user = await db_manager.get_user(user_id)
return UserResponse(
id=user["id"],
email=user["email"],
profile=user["profile"],
created_at=user["created_at"].isoformat() if user["created_at"] else ""
)
except Exception as e:
logger.error(f"创建用户失败: {str(e)}")
raise HTTPException(status_code=500, detail=str(e))
@router.get("/{user_id}", response_model=UserResponse)
async def get_user(user_id: str):
"""获取用户信息"""
try:
user = await db_manager.get_user(user_id)
if not user:
raise HTTPException(status_code=404, detail="用户不存在")
return UserResponse(
id=user["id"],
email=user["email"],
profile=user["profile"],
created_at=user["created_at"].isoformat() if user["created_at"] else ""
)
except HTTPException:
raiseView on GitHub (pinned to 606a07d341)
Solutions
- Check the '创建用户失败' log for the underlying exception (IntegrityError, ConnectionError, KeyError, AttributeError).
- If duplicate email, catch the integrity error and return 409 instead of 500.
- Verify db_manager.get_user returns the full row the response model needs after insert.
- Normalize timestamps before .isoformat() since DB drivers may return strings.
Example fix
// before
user = await db_manager.get_user(user_id)
return UserResponse(..., created_at=user["created_at"].isoformat() if user["created_at"] else "")
// after
user = await db_manager.get_user(user_id)
if not user:
raise HTTPException(status_code=500, detail="用户创建后读取失败")
created = user["created_at"]
created = created.isoformat() if hasattr(created, "isoformat") else (created or "")
return UserResponse(id=user["id"], email=user["email"], profile=user["profile"], created_at=created) Defensive patterns
Strategy: validation
Validate before calling
import json json.dumps(profile) # profile must be JSON-serializable before create
Type guard
def is_serializable_profile(p) -> bool:
try:
json.dumps(p)
return True
except TypeError:
return False Try / catch
try:
resp = await client.post("/users/", json=payload)
except httpx.HTTPError:
raise
if resp.status_code == 500 and 'duplicate' in resp.json().get('detail','').lower():
raise DuplicateUserError() Prevention
- Pre-check email uniqueness client-side where possible
- Expect 500-with-detail to carry driver messages; parse only for logging
- Reuse the creation response's user fields instead of re-GETting
When it happens
Trigger: POST create user where the database insert fails (duplicate email with a unique constraint, connection down); insert succeeds but immediate get_user read fails; returned row missing expected keys.
Common situations: Database not running or DSN misconfigured; duplicate registration with the same email; schema mismatch between db_manager's row mapping and UserResponse fields; created_at stored as string so .isoformat() raises AttributeError.
Related errors
AI-assisted analysis of datawhalechina/hello-agents@606a07d341 (2026-08-14).
Data as JSON: /api/errors/c127f64292ddbaff.
Report an issue: GitHub.