Add FieldError with field name context to parsing errors - #43
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.
|
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 |
|
Closing because this implementation is no longer release-safe after #65. The diagnostic goal is still useful, but this patch changes the old |
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