Skip to content

Commit

Permalink
Added better exception handling on save and load
Browse files Browse the repository at this point in the history
Added logging to save file to help debug errors
Fixed a bug where recipes were not being stored as expected on save load
  • Loading branch information
AndrewFahlgren committed Dec 26, 2022
1 parent 03b8ddf commit 522dc2c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 15 deletions.
26 changes: 18 additions & 8 deletions Scripts/Ex.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using PotionCraftBookmarkOrganizer.Scripts.Storage;
using System;
using System.Collections.Generic;
using System.Text;

Expand All @@ -15,15 +16,15 @@ public static class Ex
/// <param name="action">the code to run.</param>
/// <param name="errorAction">optional. Runs this code on error if provided.</param>
/// <returns>true on error unless an errorAction is specified</returns>
public static bool RunSafe(Func<bool> action, Func<bool> errorAction = null)
public static bool RunSafe(Func<bool> action, Func<bool> errorAction = null, bool logErrorToStorage = false)
{
try
{
return action();
}
catch (Exception ex)
{
LogException(ex);
LogException(ex, logErrorToStorage);
}
if (errorAction == null)
return true;
Expand All @@ -37,23 +38,32 @@ public static bool RunSafe(Func<bool> action, Func<bool> errorAction = null)
/// <param name="action">the code to run.</param>
/// <param name="errorAction">optional. Runs this code on error if provided.</param>
/// <returns>true on error unless an errorAction is specified</returns>
public static void RunSafe(Action action, Action errorAction = null)
public static void RunSafe(Action action, Action errorAction = null, bool logErrorToStorage = false)
{
try
{
action();
}
catch (Exception ex)
{
LogException(ex);
LogException(ex, logErrorToStorage);
if (errorAction != null) errorAction();
}
}

public static void LogException(Exception ex)
public static void LogException(Exception ex, bool logErrorToStorage = false)
{
var errorMessage = $"{ex.GetType()}: {ex.Message}\r\n{ex.StackTrace}\r\n{ex.InnerException?.Message}";
Plugin.PluginLogger.LogError(errorMessage);
var exceptionText = GetExceptionText(ex);
Plugin.PluginLogger.LogError(exceptionText);
if (logErrorToStorage)
{
StaticStorage.ErrorLog.Add(exceptionText);
}
}

public static string GetExceptionText(Exception ex)
{
return $"{DateTime.UtcNow}: {ex.GetType()}: {ex.Message}\r\n{ex.StackTrace}\r\n{ex.InnerException?.Message}";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class BookmarkController_LoadFrom
{
static void Postfix(BookmarkController __instance)
{
Ex.RunSafe(() => RemoveSubBookmarksFromMainRailsOnLoad( __instance));
Ex.RunSafe(() => RemoveSubBookmarksFromMainRailsOnLoad( __instance), null, true);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ private static bool AssignSubBookmarksToRailsBeforeSerialization(ref SerializedB
var allSubBookmarks = StaticStorage.BookmarkGroups.SelectMany(bg => bg.Value).ToList();
allSubBookmarks.ForEach(bookmark =>
{
var spawnPosition = SubRailService.GetSpawnPosition(instance, SpaceType.Large)
?? SubRailService.GetSpawnPosition(instance, SpaceType.Medium)
?? SubRailService.GetSpawnPosition(instance, SpaceType.Small)
?? SubRailService.GetSpawnPosition(instance, SpaceType.Min);
var spawnPosition = GetSpawnPosition(instance, SpaceType.Large, subBookmarkPositions)
?? GetSpawnPosition(instance, SpaceType.Medium, subBookmarkPositions)
?? GetSpawnPosition(instance, SpaceType.Small, subBookmarkPositions)
?? GetSpawnPosition(instance, SpaceType.Min, subBookmarkPositions);
if (!subBookmarkPositions.ContainsKey(spawnPosition.Item1))
{
Expand All @@ -61,7 +61,7 @@ private static bool AssignSubBookmarksToRailsBeforeSerialization(ref SerializedB
serialized.serializedRails = instance.rails.Select(rail => GetSerialized(rail, subBookmarkPositions)).ToList();
//Since we messed with the order of the bookmarks we need to change the saved recipe order to ensure recipes are in their proper bookmarks on load (if the mod gets uninstalled)
RearrangeSavedBookmarksToWorkForBookmarkOrder(serialized, subBookmarkPositions);
});
}, null, true);
result = serialized;
return false;
}
Expand Down
4 changes: 3 additions & 1 deletion Scripts/Services/SaveLoadService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static void StoreBookmarkGroups(ref string result)
var toSerialize = new StaticStorage.SavedStaticStorage
{
BookmarkGroups = StaticStorage.BookmarkGroups,
SavedRecipePositions = StaticStorage.SavedRecipePositions
SavedRecipePositions = StaticStorage.SavedRecipePositions,
ErrorLog = StaticStorage.ErrorLog,
BookmarkManagerVersion = Plugin.PLUGIN_GUID
};
var serializedGroups = JsonConvert.SerializeObject(toSerialize, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Expand Down
3 changes: 3 additions & 0 deletions Scripts/Storage/StaticStorage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public static class StaticStorage

public static Dictionary<int, List<BookmarkStorage>> BookmarkGroups = new();
public static List<int> SavedRecipePositions;
public static List<string> ErrorLog = new();

public static BookmarkRail SubRail;
public static GameObject SubRailPages;
Expand All @@ -41,6 +42,8 @@ public class SavedStaticStorage
{
public Dictionary<int, List<BookmarkStorage>> BookmarkGroups { get; set; }
public List<int> SavedRecipePositions { get; set; }
public List<string> ErrorLog { get; set; }
public string BookmarkManagerVersion { get; set; }
}
}
}

0 comments on commit 522dc2c

Please sign in to comment.