Add FieldError with field name context to parsing errors#43
Open
Cvikli wants to merge 3 commits intoJuliaServices:mainfrom
Open
Add FieldError with field name context to parsing errors#43Cvikli wants to merge 3 commits intoJuliaServices:mainfrom
Cvikli wants to merge 3 commits intoJuliaServices:mainfrom
Conversation
Previously, when struct parsing failed, errors like MethodError or
TypeError were thrown without any indication of which field caused the
failure. For example, parsing `{"path": 123}` into a struct with
`path::String` would produce:
MethodError: Cannot `convert` an object of type Int64 to an object of type String
Now the same error produces:
FieldError: failed to parse field `path::String` of `Bar`: MethodError: Cannot `convert`...
This applies to all failure modes:
- Type mismatches: `FieldError: failed to parse field `count::Int64` of `Bar`: MethodError...`
- Missing required fields: `FieldError: failed to parse field `count::Int64` of `Bar`: missing required field`
- null for non-nullable: `FieldError: failed to parse field `path::String` of `Bar`: MethodError...`
- Nested struct failures: `FieldError: failed to parse field `inner::Inner` of `Outer`: ...`
The FieldError exception is exported and contains `struct_type`, `field_name`,
`field_type`, and `error` fields for programmatic access.
Member
|
Hmmmmm............this looks like the right direction. I'm mostly worried about performance implications here. This code can be super performance-sensitive because it powers, for example, the raw |
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
When struct parsing fails, errors like
MethodErrororTypeErrorare thrown without any indication of which field caused the failure. This makes debugging JSON parsing issues unnecessarily difficult, especially with larger structs.This PR adds a
FieldErrorexception type that wraps the original error with field context.Before
No indication of which field failed or which struct was being parsed.
After
Implementation
FieldError <: Exception— exported, containsstruct_type,field_name,field_type, anderrorfields for programmatic access_make_fieldhelper — wraps themake()+setval!()calls infindfield()with a try-catch that adds field contextmakestruct— detects missing required fields before_constructis called, giving a clear error instead of a crypticTypeErrorFieldErrors from nested structs are rethrown as-is (no double-wrapping)Test plan