Skip to content

fix(api): Handle invalid YAML input as HTTP 400#10

Draft
sentry[bot] wants to merge 1 commit into
devfrom
seer/fix/yaml-input-validation
Draft

fix(api): Handle invalid YAML input as HTTP 400#10
sentry[bot] wants to merge 1 commit into
devfrom
seer/fix/yaml-input-validation

Conversation

@sentry

@sentry sentry Bot commented May 19, 2026

Copy link
Copy Markdown
Contributor

This PR addresses issue JAVASCRIPT-NEXTJS-4: "Error during optimization: nurse_scheduling.models.NurseSchedulingData() argument after ** must be a mapping, not str".

Problem:
When a user submits yaml_content that is a plain scalar string (e.g., "Extra inputs are not permitted"), the _load_yaml function in core/nurse_scheduling/loader.py correctly parses it as a Python string. However, the subsequent call NurseSchedulingData(**data) expects data to be a mapping (dictionary) for argument unpacking. This mismatch leads to a TypeError: argument after ** must be a mapping, not str.

Previously, this TypeError was caught by a generic except Exception block in core/nurse_scheduling/serve.py, resulting in an HTTP 500 Internal Server Error, which is misleading for a client-side input validation issue.

Solution:

  1. Introduce InputError: A new custom exception InputError (inheriting from ValueError) is defined in core/nurse_scheduling/loader.py to specifically signal user-input validation failures.
  2. Validate YAML in loader.py:
    • loader.load_data() now explicitly checks if the result of _load_yaml(content) is a dictionary. If not, or if the YAML parsing itself fails, it raises an InputError with a descriptive message.
    • It also wraps TypeError exceptions that occur during NurseSchedulingData(**data) (e.g., due to malformed dictionary structure) into an InputError.
  3. Handle InputError in serve.py:
    • The optimize_and_export_xlsx endpoint in core/nurse_scheduling/serve.py now includes a specific except InputError block.
    • When an InputError is caught, it raises an HTTPException with status_code=400 (Bad Request), providing a clear indication that the issue is with the user's input.
    • The existing generic except Exception block remains to catch and log genuine server-side errors, returning an HTTP 500.

Impact:
This change improves the API's robustness and user experience by providing accurate HTTP status codes and clearer error messages for invalid YAML input, distinguishing them from unexpected server errors.

Fixes JAVASCRIPT-NEXTJS-4

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.

0 participants