fix(api): Handle invalid YAML input as HTTP 400#10
Draft
sentry[bot] wants to merge 1 commit into
Draft
Conversation
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.
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_contentthat is a plain scalar string (e.g.,"Extra inputs are not permitted"), the_load_yamlfunction incore/nurse_scheduling/loader.pycorrectly parses it as a Python string. However, the subsequent callNurseSchedulingData(**data)expectsdatato be a mapping (dictionary) for argument unpacking. This mismatch leads to aTypeError: argument after ** must be a mapping, not str.Previously, this
TypeErrorwas caught by a genericexcept Exceptionblock incore/nurse_scheduling/serve.py, resulting in an HTTP 500 Internal Server Error, which is misleading for a client-side input validation issue.Solution:
InputError: A new custom exceptionInputError(inheriting fromValueError) is defined incore/nurse_scheduling/loader.pyto specifically signal user-input validation failures.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 anInputErrorwith a descriptive message.TypeErrorexceptions that occur duringNurseSchedulingData(**data)(e.g., due to malformed dictionary structure) into anInputError.InputErrorinserve.py:optimize_and_export_xlsxendpoint incore/nurse_scheduling/serve.pynow includes a specificexcept InputErrorblock.InputErroris caught, it raises anHTTPExceptionwithstatus_code=400(Bad Request), providing a clear indication that the issue is with the user's input.except Exceptionblock 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