Skip to content

refactor(backend): move service-layer HTTP errors to API boundary mapping#366

Merged
peteski22 merged 3 commits into
mainfrom
refactor/service-error-boundaries
May 12, 2026
Merged

refactor(backend): move service-layer HTTP errors to API boundary mapping#366
peteski22 merged 3 commits into
mainfrom
refactor/service-error-boundaries

Conversation

@peteski22
Copy link
Copy Markdown
Collaborator

Summary

  • Introduce a service-layer exception hierarchy and domain exceptions for auth, API keys, knowledge, review, and users.
  • Remove fastapi.HTTPException usage from backend services and raise service/domain errors instead.
  • Map service exceptions to HTTP status codes at route/app boundary, including centralized handling in cq_server.app.

Validation

  • make lint
  • make test

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.exceptions hierarchy (auth, API keys, knowledge, review, users) and updated services to raise these errors instead of HTTPException.
  • Added centralized ServiceError → HTTP status mapping via a global FastAPI exception handler in cq_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.

Comment thread server/backend/src/cq_server/api/routes/users.py
Comment thread server/backend/src/cq_server/api/routes/knowledge.py
@peteski22 peteski22 merged commit 48221d0 into main May 12, 2026
9 checks passed
@peteski22 peteski22 deleted the refactor/service-error-boundaries branch May 12, 2026 20:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants