Skip to content

refactor: rely on global exception handlers in route modules - #289

Open
emiliano-go wants to merge 3 commits into
benavlabs:mainfrom
emiliano-go:refactor/route-error-handling-v2
Open

emiliano-go wants to merge 3 commits into
benavlabs:mainfrom
emiliano-go:refactor/route-error-handling-v2

Conversation

@emiliano-go

Copy link
Copy Markdown
Collaborator

Most route handlers repeated the same try/except block that mapped domain exceptions and returned a 500, even though the app already registers a global DomainError handler and a catch-all middleware with the same mapping. The copies had also diverged: several api_keys routes leaked raw exception text in 404 and 403 responses, and two different generic 500 messages coexisted.

This PR removes the per-route boilerplate from the user, api_keys, rate_limit and tier routers and lets exceptions propagate to the global handlers. It also removes dead if user is None 404 checks (services raise UserNotFoundError instead of returning None) and makes delete_user_account check permissions before the user lookup, matching update_user_profile; this intentionally changes the response for deleting a non-existent user from 404 to 403 so the API does not reveal which usernames exist.

Behavior note: 403 and 404 responses sourced from domain errors now return the generic message with a support id instead of specific details, which is the policy the global handler already enforced.

Depends on #284 (read-endpoint-auth), which touches the same route files; merge that one first.

Written by Kimi, Authored by @emiliano-go

…ute try/except

Route handlers in user, api_keys, rate_limit, and tier modules repeated
the same handle_exception boilerplate that diverged from the global
error policy (some api_keys routes leaked str(e) in 404/403 details,
and 500s used an ad-hoc message). The global DomainError handler and
CatchAllErrorMiddleware registered in app_factory already cover every
exception type these blocks handled, so routes now let exceptions
propagate. Generic 500 wording is unified on GENERIC_ERROR_MESSAGE.
Services raise UserNotFoundError instead of returning None, so the
'if user is None' 404 branches were dead code. delete_user_account now
verifies permission before the user lookup, matching update_user_profile
and avoiding username enumeration via 404-vs-403 responses.
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.

1 participant