Skip to content

Harden build_line_item_summary against chaos key-rename injection + add unit tests#14

Draft
Copilot wants to merge 2 commits into
mainfrom
copilot/inc-20260505-fix-keyerror-price
Draft

Harden build_line_item_summary against chaos key-rename injection + add unit tests#14
Copilot wants to merge 2 commits into
mainfrom
copilot/inc-20260505-fix-keyerror-price

Conversation

Copy link
Copy Markdown

Copilot AI commented May 5, 2026

Recurring chaos injection renames unit_priceprice in build_line_item_summary(), causing KeyError crashes on every POST /api/demo/simulate-load and POST /api/orders/{id}/process call.

Changes

apps/api/app/routes/orders.py

  • Replace direct key access with .get() + safe defaults throughout build_line_item_summary() — eliminates KeyError regardless of field name mutation
  • Emit logger.warning when unit_price or quantity are absent, making chaos injection immediately visible in Application Insights without crashing
# Before — crashes on chaos injection
total = item["unit_price"] * item["quantity"]

# After — degrades gracefully, warns in logs
if "unit_price" not in item:
    logger.warning("build_line_item_summary: missing 'unit_price' key in item %s; defaulting to 0.0", ...)
unit_price = item.get("unit_price", 0.0)
quantity   = item.get("quantity", 0)
total      = unit_price * quantity

apps/api/tests/test_orders.py (new)

Five unit tests covering: correct keys, chaos "price" key (no KeyError), missing quantity (no ZeroDivisionError), zero quantity, and missing sku/name fallbacks.

Copilot AI changed the title [WIP] Fix KeyError for 'price' in chaos bug re-injection Harden build_line_item_summary against chaos key-rename injection + add unit tests May 5, 2026
Copilot AI requested a review from jonathanscholtes May 5, 2026 12:39
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.

[INC-20260505-1222-a1c4] KeyError: 'price' chaos bug re-injection + Cosmos DB public network access disabled

2 participants