fix(proto-plus): add context to TypeErrors during message manipulation#17682
Open
guptaarima wants to merge 1 commit into
Open
fix(proto-plus): add context to TypeErrors during message manipulation#17682guptaarima wants to merge 1 commit into
guptaarima wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request improves error handling in packages/proto-plus/proto/message.py by wrapping protocol buffer initialization and field assignment operations in try-except blocks. When a TypeError occurs during these operations, a more descriptive TypeError is raised with context about the class name, field key, and the underlying error. I have no feedback to provide as there are no review comments.
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 of the issue:
Context:
When using proto-plus, if a user assigns a value of an incorrect type to a message field (e.g., assigning a List[int] to a field expecting a MutableSequence[str]), the error raised originates from the underlying protocol buffer library and lacks specific details about the source of the error within the proto-plus message structure.
Actual Behavior:
The library propagates a generic TypeError from the core protobuf implementation (e.g., TypeError: bad argument type for built-in operation). This error message is cryptic because it does not identify which message class or field name received the invalid type, making it difficult for users to pinpoint the cause of the type mismatch in their code.
Expected Behavior:
proto-plus should catch these underlying TypeError exceptions during field assignments or message merging (like MergeFrom). It should then raise a new, more informative TypeError that includes context about the specific proto-plus message class and the field name involved.
For example, an improved error message could look like:
TypeError: Invalid type for field 'field_name' in message 'MessageName'. Expected but got .
This fix will significantly improve the debuggability of type-related errors when using proto-plus.