Skip to content

fix: replace broad except Exception with specific error handling#22

Open
themavik wants to merge 1 commit intonetdevops:mainfrom
themavik:fix/specific-exception-handling-11
Open

fix: replace broad except Exception with specific error handling#22
themavik wants to merge 1 commit intonetdevops:mainfrom
themavik:fix/specific-exception-handling-11

Conversation

@themavik
Copy link

@themavik themavik commented Feb 11, 2026

Summary

Fixes #11

Every router endpoint catches Exception and returns HTTP 400, masking actual server errors (TypeError, AttributeError, etc.) as "bad request" responses and swallowing tracebacks.

Root Cause

The pattern used across all five router modules:

except Exception as e:
    raise HTTPException(status_code=400, detail=f"Failed to ...: {str(e)}") from e

This conflates client input errors with server-side bugs. A TypeError from a code defect is reported as a 400 instead of a 500, and the original traceback is lost from the client's perspective.

Changes

Across batch.py, configs.py, platforms.py, remediation.py, and reports.py:

  1. Narrowed exception handlers from except Exception to except (ValueError, KeyError) — these are the expected input-validation failures (invalid platform names, missing fields, unsupported formats) that warrant a 400 response.

  2. Removed try/except entirely from endpoints that access known-good stored data and have no expected failure mode (get_batch_job_status, get_report_summary, list_platforms). These already validate inputs before the service call (e.g. 404 for missing jobs).

  3. Unexpected exceptions propagate to FastAPI's default 500 handler, which preserves the full traceback in server logs for debugging.

Testing

  • Client input errors (invalid platform, bad config text) still return 400 with descriptive messages
  • Unexpected server errors now correctly return 500 and log the full traceback
  • 404 paths (missing jobs, reports, remediations) are unchanged

Replace blanket `except Exception` handlers in all router endpoints
with targeted `except (ValueError, KeyError)` to properly distinguish
client errors (400) from server errors (500).

Previously, every endpoint caught Exception and returned HTTP 400,
which masked genuine server-side bugs (TypeError, AttributeError, etc.)
as bad-request responses and swallowed tracebacks.

Changes across all five router modules (batch, configs, platforms,
remediation, reports):

- Narrow exception handlers to (ValueError, KeyError) for 400 responses
- Remove try/except entirely from endpoints that access known-good
  stored data (get_batch_job_status, get_report_summary, list_platforms)
- Unexpected exceptions now propagate to FastAPI's default 500 handler
  which preserves the full traceback for debugging

Fixes netdevops#11

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

Replace broad except Exception with specific error handling

1 participant