Skip to content

fix(api): map core engine errors to 400 and cap stream payload - #112

Merged
CameronBrooks11 merged 1 commit into
mainfrom
fix/api-error-handling
Jun 24, 2026
Merged

CameronBrooks11 merged 1 commit into
mainfrom
fix/api-error-handling

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

What

Map recoverable core-engine input errors in the API to HTTP 400 instead of letting them surface as raw 500s, and cap the /stream payload size.

Why

The plan and simulate routes called plan_wind() / simulate_program() with no exception handling, so a bad-but-recoverable input (LayerValidationError, SimulationError) became an opaque 500. The CLI treats these as clean user errors; the API now matches.

How

  • Centralized handlers in create_app() (main.py): add_exception_handler for PlanningError (covers its subclass LayerValidationError via isinstance dispatch) and SimulationError, both → 400 {"detail": str(exc)}. Centralizing is DRY, covers any future route, and structurally fixes the previously-missing SimulationError import in the simulate route (the route needs no change).
  • Both map to 400 for consistency with the existing WindFileError → 400 convention (per maintainer decision).
  • Stream payload cap: StreamRequest.gcode gets max_length=10_000_000 (~10 MB, comfortably above realistic worst-case winding programs) so oversized payloads are rejected by request validation (422) rather than consuming unbounded memory.

Verification

  • New error-path tests: plan validation error → 400 (windAngle out of range), empty program → 400 (SimulationError), oversized stream payload → 422. Each asserts != 500.
  • ruff, ruff format --check, mypy (strict), full pytest: all green (114 passed).

Closes #104

The plan and simulate routes let core-engine input errors escape as raw
HTTP 500s, unlike the CLI which treats them as clean user errors. Register
centralized FastAPI exception handlers in create_app() mapping PlanningError
(and its subclass LayerValidationError) and SimulationError to 400 with a
{"detail": ...} body. Centralizing in main.py is DRY, covers any future
route, and structurally fixes the missing SimulationError import in the
simulate route.

Also cap StreamRequest.gcode at 10_000_000 chars (pydantic max_length) so an
oversized payload is rejected with 422 rather than consuming unbounded memory.

Adds error-path tests: plan validation -> 400, empty program -> 400,
oversized stream payload -> 422.

Closes #104
@CameronBrooks11 CameronBrooks11 added the python Pull requests that update Python code label Jun 24, 2026
@CameronBrooks11
CameronBrooks11 merged commit b57cffb into main Jun 24, 2026
18 checks passed
@CameronBrooks11
CameronBrooks11 deleted the fix/api-error-handling branch June 24, 2026 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Harden API route error handling (plan/simulate 500s, stream payload cap)

1 participant