feat(json): propagate field name in FromJsonAny deserialization errors - #467
feat(json): propagate field name in FromJsonAny deserialization errors#467AAmbuj wants to merge 1 commit into
Conversation
|
The created documentation from the pull request is available at: docu-html |
There was a problem hiding this comment.
Pull request overview
Adds struct field names to JSON deserialization error messages.
Changes:
- Annotates type and missing-field errors with field names.
- Adds tests for direct, missing, and nested-field failures.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
score/json/json_serializer.h |
Adds field-name error annotation. |
score/json/json_serializer_test.cpp |
Tests field-name propagation. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| score::cpp::ignore = visitor.error.emplace( | ||
| MakeError(static_cast<Error>(*field_content.error()), field_name)); |
There was a problem hiding this comment.
Fixed. Added Error::WithUserMessage() to error.h which returns a copy preserving the original numeric code and ErrorDomain*, replacing only user_messages_. JsonDeserializeStructImpl now calls field_content.error().WithUserMessage(field_name) instead of MakeError(static_castjson::Error(...))json::Error(...)), so a TypedHash field failure carrying score::hash::ErrorCode::kInvalidParameters propagates with its hash domain intact.
bb0a5d1 to
17d5a89
Compare
|
Documentation preview for this pull request is available at: |
17d5a89 to
756d896
Compare
JsonDeserializeStructImpl now annotates every deserialization failure with the name of the struct field that caused it, stored in the error's UserMessage(). Previously, field identity was lost and only a generic error code was returned. - Type mismatch: UserMessage() = failing field name (e.g. "pdu_id") - Missing mandatory field: UserMessage() = absent field name - Nested struct failure: UserMessage() = outermost containing field name field_name is a static const char* from struct_visitable, so no heap allocation occurs and no string_view lifetime issue is introduced. Add three tests covering type mismatch, missing key, and nested struct error propagation cases.
756d896 to
7fa9914
Compare
| // When deserializing into the struct | ||
| auto unit{FromJsonAny<TypeToSerialize>(std::move(source))}; | ||
|
|
||
| // Then the error code is WrongType and the UserMessage names the parent struct field, not the inner field |
There was a problem hiding this comment.
Shouldn't we add error for specific error field in nested struct, ie. "nested_bool" , "next_int" etc.
instead of "nested_value" which is again kind of generic error ?
JsonDeserializeStructImpl now annotates every deserialization failure with the name of the struct field that caused it, stored in the error's UserMessage(). Previously, field identity was lost and only a generic error code was returned.
field_name is a static const char* from struct_visitable, so no heap allocation occurs and no string_view lifetime issue is introduced.
Add three tests covering type mismatch, missing key, and nested struct error propagation cases.