fix: replace broad except Exception with specific error handling#22
Open
themavik wants to merge 1 commit intonetdevops:mainfrom
Open
fix: replace broad except Exception with specific error handling#22themavik wants to merge 1 commit intonetdevops:mainfrom
themavik wants to merge 1 commit intonetdevops:mainfrom
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #11
Every router endpoint catches
Exceptionand 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:
This conflates client input errors with server-side bugs. A
TypeErrorfrom 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, andreports.py:Narrowed exception handlers from
except Exceptiontoexcept (ValueError, KeyError)— these are the expected input-validation failures (invalid platform names, missing fields, unsupported formats) that warrant a 400 response.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).Unexpected exceptions propagate to FastAPI's default 500 handler, which preserves the full traceback in server logs for debugging.
Testing