Skip to content
Sarj Code Standards

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

Before — flagged A service raises an HTTP error
app/services/orders.py
from fastapi import HTTPException
def cancel(order) -> None:
if order.shipped:
raise HTTPException(status_code=409, detail="order_shipped")
After — preferred A service raises a domain exception
app/services/orders.py
class OrderShippedError(Exception):
pass
def cancel(order) -> None:
if order.shipped:
raise OrderShippedError