no-http-exception-in-services
Raise domain exceptions in service modules; map them to HTTP at the router.
Why
A service that raises HTTPException fixes a status code for every caller, so workers, schedulers and other services that reuse it receive an HTTP error outside any request.
Fix
Raise a domain exception from the service and convert it to a status in the router or a registered exception handler.
Examples
from fastapi import HTTPException
def cancel(order) -> None: if order.shipped: raise HTTPException(status_code=409, detail="order_shipped")class OrderShippedError(Exception): pass
def cancel(order) -> None: if order.shipped: raise OrderShippedError