Skip to content

Commit

Permalink
Human Readable JSON Error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ActuallyTrent committed Jul 29, 2016
1 parent c69d60a commit be2f597
Showing 1 changed file with 46 additions and 22 deletions.
68 changes: 46 additions & 22 deletions PoGo.NecroBot.Logic/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using PokemonGo.RocketAPI.Enums;
using POGOProtos.Enums;
using POGOProtos.Inventory.Item;

using PoGo.NecroBot.Logic.Logging;
#endregion

namespace PoGo.NecroBot.CLI
Expand All @@ -31,21 +31,37 @@ internal class AuthSettings

public void Load(string path)
{
_filePath = path;

if (File.Exists(_filePath))
try
{
//if the file exists, load the settings
var input = File.ReadAllText(_filePath);

var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });

JsonConvert.PopulateObject(input, this, settings);
_filePath = path;

if (File.Exists(_filePath))
{
//if the file exists, load the settings
var input = File.ReadAllText(_filePath);

var settings = new JsonSerializerSettings();
settings.Converters.Add(new StringEnumConverter { CamelCaseText = true });

JsonConvert.PopulateObject(input, this, settings);
}
else
{
Save(_filePath);
}
}
else
catch(Newtonsoft.Json.JsonReaderException exception)
{
Save(_filePath);
if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("PtcUsername"))
Logger.Write("JSON Exception: You need to properly configure your PtcUsername using quotations.", LogLevel.Error);
else if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("PtcPassword"))
Logger.Write("JSON Exception: You need to properly configure your PtcPassword using quotations.", LogLevel.Error);
else if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("GoogleUsername"))
Logger.Write("JSON Exception: You need to properly configure your GoogleUsername using quotations.", LogLevel.Error);
else if (exception.Message.Contains("Unexpected character") && exception.Message.Contains("GooglePassword"))
Logger.Write("JSON Exception: You need to properly configure your GooglePassword using quotations.", LogLevel.Error);
else
Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
}
}

Expand Down Expand Up @@ -285,15 +301,23 @@ public static GlobalSettings Load(string path)

if (File.Exists(configFile))
{
//if the file exists, load the settings
var input = File.ReadAllText(configFile);

var jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;

settings = JsonConvert.DeserializeObject<GlobalSettings>(input, jsonSettings);
try
{
//if the file exists, load the settings
var input = File.ReadAllText(configFile);

var jsonSettings = new JsonSerializerSettings();
jsonSettings.Converters.Add(new StringEnumConverter { CamelCaseText = true });
jsonSettings.ObjectCreationHandling = ObjectCreationHandling.Replace;
jsonSettings.DefaultValueHandling = DefaultValueHandling.Populate;

settings = JsonConvert.DeserializeObject<GlobalSettings>(input, jsonSettings);
}
catch (Newtonsoft.Json.JsonReaderException exception)
{
Logger.Write("JSON Exception: " + exception.Message, LogLevel.Error);
return null;
}
}
else
{
Expand Down

0 comments on commit be2f597

Please sign in to comment.