siscodeorg / sisbase

An easy to use discord bot base for D#+. Discord Link :
https://discord.gg/ycvm6xJ
MIT License
1 stars 0 forks source link

Safely handle malformed/invalid config files #34

Closed wffirilat closed 4 years ago

wffirilat commented 4 years ago

Current behaviour: The program will crash with an NRE when an invalid config file exists.

Ideal behaviour: The program will backup the existing malformed config file, print a warning, possibly give some information about why it is malformed, and then generate a new file with the default settings

alikindsys commented 4 years ago

Malformed files can come in two types. Invalid JSON and Invalid Schema. Whilst Invalid JSON is detectable (since it throws an exception), Invalid Schema isn't.

Notes about Invalid Schema

JToken#.ToType<T>() will create a new instance of T no matter the serialization of the underlying token.

Using JsonConvert.Deserialize<T>(#JToken) has a simmilar effect, which leads me to believe that JToken#.ToType<T>() is how it is implemented.

Attempting to try-catch any JSONException was in vain. Same goes for Exception. Json.NET doesn't throw an exception for invalid schema.

There seems to exist a Json.NET.Schema package that has an JToken#IsValid(JsonSchema) extension method.

alikindsys commented 4 years ago

A Fix for this issue is already implemented but its pending review. Branch : 33-main-config-porting Commit : https://github.com/siscodeorg/sisbase/commit/c4ce7ae0a1988f05b980c2cc6379ed617aad61b5

wffirilat commented 4 years ago

This issue is entirely superseded by the scope of #33. closed.