-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve error message for validation errors in System.Test.Json deserialization #723
Comments
Thanks for suggestion @Peter-B- - it would certainly make diagnosing issues easier. The serializer would have to catch all exceptions though as it's possible to specify a different exception type. But either way, a |
Hello @SteveDunn, thank you for considering this. I was just looking at how to implement it. I see that all templates invoke My idea is, to add another method // only called internally from System.Text.Json Converter
private static Dave __DeserializeSystemTextJson(System.String value)
{
try
{
return Dave.__Deserialize(value);
}
catch (System.Exception e)
{
throw new global::System.Text.Json.JsonException(null, e);
}
} All SystemTextJsonConverter-templates can be adapted to use this type. Do you think this would be a good way to implement this? I'd also like to create some test cases for that - a JsonException being thrown. Should that go next to |
Hi @Peter-B- thank you. Your suggestion sounds good. Perhaps Re. the test cases - yes, that would be perfect! Thanks again! Steve |
Thank you for the review of this proposal. I will try to implement it. |
Describe the feature
Hello,
I use value objects with validation in combination with System.Text.Json. I have a proposal to improve error messages for failed validation.
Current state
When the validation of a value object fails, a
ValueObjectValidationException
is thrown. However, no information about the context within the Json is provided.This leads to
Within complex Jsons with multiple value objects, the error is hard to pin down.
Proposal
System.Text.Json has special handling for JsonExceptions. If the
Message
is null, the context within the Json will automatically be provided.Here is an example with a custom
JsonConverter
that wraps theValueObjectValidationException
within aJsonException
.Tested with
the following exception will be printed:
Please note that the path within the Json is printed within the exception:
Improvements
If this feature is implemented, there are some ways to further improve this:
TryParse
method to avoid two exceptions being thrownPath
contextWhat do you think?
The text was updated successfully, but these errors were encountered: