fix(core, api): Return 400 for non-mapping YAML input#19
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 an issue where submitting non-mapping YAML content to the
/optimize-and-export-xlsxendpoint resulted in a generic 500 Internal Server Error instead of a more appropriate 400 Bad Request.Changes Made:
core/nurse_scheduling/loader.py: Modified theload_datafunction to validate the type of the parsed YAML content. If_load_yamlreturns a non-mapping (e.g., a bare scalar string), aValueErroris now raised.core/nurse_scheduling/serve.py: Added a specificexcept ValueErrorblock in theoptimize_and-export-xlsxendpoint's error handling. This block catches theValueErrorraised byload_dataand translates it into anHTTPExceptionwith a 400 status code and a clear, user-friendly detail message.Mappingfromcollections.abcinloader.pyfor type checking.Why these changes:
Previously, when a user submitted YAML content that was a simple scalar (like a plain string) instead of a dictionary, the
NurseSchedulingData(**data)call would fail with aTypeError(argument after**must be a mapping, not str). ThisTypeErrorwas then caught by a broadexcept Exceptionblock, leading to a misleading 500 error. By explicitly checking for a mapping type and raising aValueError, we can now provide a precise 400 Bad Request response, indicating that the client's input format was incorrect, thereby improving API clarity and user experience.Fixes NURSE-SCHEDULING-5