Improve netkan relationship error message #4020
Merged
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.
Problem
This metadata fragment:
... produces an obscure inflation error:
This example is from KSP-CKAN/NetKAN#9909.
Cause
An exception is thrown on this line of
RelationshipsValidator
:There are four main ways to loop over the children of a JSON array property:
.Cast<T>()
, which is a generic Linq function and throws an exception if any element isn't aT
("BDArmoryForRunwayProject" is aJValue
, not aJObject
).OfType<T>()
, which is a generic Linq function and ignores any element that isn't aT
instead of throwing.Children<T>()
, which is aNewtonsoft.Json
function and equivalent to.OfType<T>()
.Children()
, which is aNewtonsoft.Json
function and equivalent to.Children<JToken>()
We're using the wrong option from this list.
Changes
.Children<Jobject>()
, which will examine all the object children and skip the rest. If any of the others are invalid, the schema validator will catch them.A side note, I did try catching the
InvalidCastException
, but it simply didn't work. The code that throws was within thetry{}
, thecatch (InvalidCastException){}
was immediately afterwards, but it continued to be caught by the outer code. I was not able to figure out why that happened.