-
-
Notifications
You must be signed in to change notification settings - Fork 85
fix: handle HTTPException in get_item function #1259
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
WalkthroughError handling in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (1 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Disabled knowledge base sources:
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (1)
src/routers/secure/items.py(1 hunks)
🧰 Additional context used
🪛 Ruff (0.14.3)
src/routers/secure/items.py
404-404: Use raise without specifying exception name
Remove exception name
(TRY201)
| if isinstance(e, HTTPException): | ||
| raise e |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion | 🟠 Major
Fix correctly preserves HTTPException semantics, but consider two improvements.
The change successfully prevents HTTPException instances (like the 404 at line 401) from being swallowed and re-wrapped as 500 errors.
However:
- Use bare
raiseinstead ofraise eto preserve the original traceback:
if isinstance(e, HTTPException):
- raise e
+ raise- Consider narrowing the try-except scope. The current try block (lines 394-415) wraps both the database query (line 395) and business logic (lines 396-401). This causes the HTTPException at line 401 to be caught unnecessarily. A cleaner structure would separate concerns:
try:
item: MediaItem = session.execute(query).unique().scalar_one_or_none()
except Exception as e:
# Handle only database exceptions here
if "Multiple rows were found" in str(e):
items = session.execute(query).unique().scalars().all()
duplicate_ids = {item.id for item in items}
logger.debug(f"Multiple items found with ID {id}: {duplicate_ids}")
raise HTTPException(
status_code=500,
detail=f"Multiple items found with ID {id}: {duplicate_ids}",
)
logger.error(f"Error fetching item with ID {id}: {str(e)}")
raise HTTPException(status_code=500, detail=str(e)) from e
# Business logic outside try block (no HTTPException re-raising needed)
if item:
if extended:
return item.to_extended_dict()
return item.to_dict()
else:
raise HTTPException(status_code=404, detail="Item not found")Based on static analysis hints.
🧰 Tools
🪛 Ruff (0.14.3)
404-404: Use raise without specifying exception name
Remove exception name
(TRY201)
dreulavelle
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you dont need the original traceback then its fine, otherwise might want to swap to raise instead of raise e.
| else: | ||
| raise HTTPException(status_code=404, detail="Item not found") | ||
| except Exception as e: | ||
| if isinstance(e, HTTPException): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just except this?
Pull Request Check List
Resolves: #issue-number-here
Description:
Summary by CodeRabbit