Skip to content
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 netkan relationship error message #4020

Merged
merged 1 commit into from
Feb 10, 2024

Conversation

HebaruSan
Copy link
Member

@HebaruSan HebaruSan commented Feb 10, 2024

Problem

This metadata fragment:

depends:
  - BDArmoryForRunwayProject

... produces an obscure inflation error:

362 [1] FATAL CKAN.NetKAN.Program (null) - Specified cast is not valid.

This example is from KSP-CKAN/NetKAN#9909.

Cause

An exception is thrown on this line of RelationshipsValidator:

                    foreach (JObject rel in json[relName].Cast<JObject>())

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 a T ("BDArmoryForRunwayProject" is a JValue, not a JObject)
  • .OfType<T>(), which is a generic Linq function and ignores any element that isn't a T instead of throwing
  • .Children<T>(), which is a Newtonsoft.Json function and equivalent to .OfType<T>()
  • .Children(), which is a Newtonsoft.Json function and equivalent to .Children<JToken>()

We're using the wrong option from this list.

Changes

  • Now we use .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.
  • The schema validator now outputs all on one line so the GitHub action can parse it better
    $ netkan.exe NetKAN/kOS-KerbalEngineer.netkan
    1717 [1] FATAL CKAN.NetKAN.Program (null) - Schema validation failed: #/depends[0]: ArrayItemNotValid, #/depends[1]: ArrayItemNotValid, #/depends[2]: ArrayItemNotValid, #/supports[0]: ArrayItemNotValid, #/supports[1]: ArrayItemNotValid
    

A side note, I did try catching the InvalidCastException, but it simply didn't work. The code that throws was within the try{}, the catch (InvalidCastException){} was immediately afterwards, but it continued to be caught by the outer code. I was not able to figure out why that happened.

@HebaruSan HebaruSan added Bug Easy This is easy to fix Netkan Issues affecting the netkan data Relationships Issues affecting depends, recommends, etc. labels Feb 10, 2024
@HebaruSan HebaruSan merged commit c91a46f into KSP-CKAN:master Feb 10, 2024
8 checks passed
@HebaruSan HebaruSan deleted the fix/netkan-rel-msg branch February 10, 2024 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Easy This is easy to fix Netkan Issues affecting the netkan data Relationships Issues affecting depends, recommends, etc.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant