evals / debt-shop
raise-from-translation
Expected
Hidden from the reviewer, like the grading note below.
- no-swallowed-errors
- holds
handle_order translates pydantic's ValidationError into BadRequest with raise ... from err, which the contract names as allowed. api.py had no handlers before. The contract holds.
Change
shop/api.py
-1+10
1234567891011121371415161718from pydantic import ValidationError
from shop.models import Orderfrom shop.orders import place_orderfrom shop.store import Store
class BadRequest(Exception): pass
def handle_order(store: Store, body: str) -> str: try: order = Order.model_validate_json(body) order = Order.model_validate_json(body) except ValidationError as err: raise BadRequest(str(err)) from err receipt = place_order(store, order) return receipt.model_dump_json()Contracts
no-swallowed-errors
---
name: no-swallowed-errors
message: Let errors crash; never catch an exception to hide it.
---
## Rules
Python code under `shop/` must not suppress exceptions.
Every `except` block must re-raise the exception or raise another exception, for example `raise BadRequest(...) from err`.
An `except` block that returns a value, returns `None`, uses `pass` or `continue`, or only logs is forbidden.
`contextlib.suppress` is forbidden for the same reason.
`try/finally` used only for cleanup, such as closing a file, is allowed.