refactor(backend): move service-layer HTTP errors to API boundary mapping#366
Merged
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR refactors the backend to stop raising fastapi.HTTPException from service-layer code, introducing a service/domain exception hierarchy and centralizing translation of those exceptions to HTTP responses at the API boundary (FastAPI app/router layer).
Changes:
- Added a
cq_server.exceptionshierarchy (auth, API keys, knowledge, review, users) and updated services to raise these errors instead ofHTTPException. - Added centralized
ServiceError→ HTTP status mapping via a global FastAPI exception handler incq_server.app. - Updated dependencies and tests to reflect the new service-layer behavior (notably API key auth and last-used tracking).
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| server/backend/tests/test_deps.py | Updates API key service tests for new authenticate() signature and touch behavior. |
| server/backend/tests/test_auth_service.py | Adds unit tests for AuthService.login raising service-layer exceptions. |
| server/backend/src/cq_server/services/reviews.py | Replaces HTTPException with review/knowledge service exceptions. |
| server/backend/src/cq_server/services/knowledge.py | Replaces HTTPException with knowledge service exceptions and updates docstrings. |
| server/backend/src/cq_server/services/auth.py | Replaces HTTPException with InvalidCredentialsError. |
| server/backend/src/cq_server/services/api_keys.py | Replaces HTTPException with API key/user service exceptions; splits out touch_last_used. |
| server/backend/src/cq_server/exceptions/base.py | Introduces base ServiceError and HTTP-category subclasses. |
| server/backend/src/cq_server/exceptions/auth.py | Adds auth-specific service exceptions. |
| server/backend/src/cq_server/exceptions/api_keys.py | Adds API key-specific service exceptions. |
| server/backend/src/cq_server/exceptions/knowledge.py | Adds knowledge-specific service exceptions. |
| server/backend/src/cq_server/exceptions/review.py | Adds review-specific service exceptions. |
| server/backend/src/cq_server/exceptions/users.py | Adds user-specific service exceptions. |
| server/backend/src/cq_server/exceptions/init.py | Exports exception types for convenient importing. |
| server/backend/src/cq_server/app.py | Adds a global FastAPI exception handler mapping ServiceError to HTTP responses. |
| server/backend/src/cq_server/api/routes/auth.py | Adds auth route exception→status mapping helper. |
| server/backend/src/cq_server/api/routes/knowledge.py | Adds knowledge route exception→status mapping helper. |
| server/backend/src/cq_server/api/routes/review.py | Adds review route exception→status mapping helper. |
| server/backend/src/cq_server/api/routes/users.py | Adds user route exception→status mapping helper. |
| server/backend/src/cq_server/api/deps.py | Moves API key last_used_at update scheduling into the dependency; converts service error to HTTPException. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
fastapi.HTTPExceptionusage from backend services and raise service/domain errors instead.cq_server.app.Validation
make lintmake test