Skip to content

Commit

Permalink
Automatic Error Collection (#603)
Browse files Browse the repository at this point in the history
# Main Goal
Add a way for Collapse to automatically report errors to a self-hosted
instance of SentrySDK compatible server
  • Loading branch information
bagusnl authored Nov 16, 2024
2 parents 73db88d + 6722a1c commit 7ac974b
Show file tree
Hide file tree
Showing 99 changed files with 846 additions and 119 deletions.
11 changes: 9 additions & 2 deletions CollapseLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using CollapseLauncher.Helper;
using CollapseLauncher.Helper.Image;
using Hi3Helper;
using Hi3Helper.SentryHelper;
using Hi3Helper.Shared.Region;
using Microsoft.UI;
using Microsoft.UI.Xaml;
Expand Down Expand Up @@ -51,9 +52,14 @@ public App()
UnhandledException += static (sender, e) =>
{
LogWriteLine($"[XAML_OTHER] Sender: {sender}\r\n{e!.Exception} {e.Exception!.InnerException}", LogType.Error, true);
#if !DEBUG
var ex = e.Exception;
if (ex != null)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledXaml);
}
#if !DEBUG
MainEntryPoint.SpawnFatalErrorConsole(e!.Exception);
#endif
#endif
};
}

Expand Down Expand Up @@ -133,6 +139,7 @@ protected override void OnLaunched(LaunchActivatedEventArgs args)
{
LogWriteLine($"FATAL ERROR ON APP INITIALIZER LEVEL!!!\r\n{ex}", LogType.Error, true);
LogWriteLine("\r\nIf this is not intended, please report it to: https://github.com/CollapseLauncher/Collapse/issues\r\nPress any key to exit...");
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
//Console.ReadLine();
throw;
}
Expand Down
9 changes: 8 additions & 1 deletion CollapseLauncher/Classes/EventsManagement/EventsHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text.Json.Serialization;
using Windows.Foundation;
using Windows.Networking.Connectivity;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.InnerLauncherConfig;
using static Hi3Helper.Locale;
using static Hi3Helper.Shared.Region.LauncherConfig;
Expand Down Expand Up @@ -133,11 +134,17 @@ internal static class ErrorSender
public static ErrorType ExceptionType;
public static string ExceptionTitle;
public static string ExceptionSubtitle;
public static void SendException(Exception e, ErrorType eT = ErrorType.Unhandled) => invoker!.SendException(e, eT);

public static void SendException(Exception e, ErrorType eT = ErrorType.Unhandled)
{
SentryHelper.ExceptionHandler(e, eT == ErrorType.Unhandled ? SentryHelper.ExceptionType.UnhandledOther : SentryHelper.ExceptionType.Handled);
invoker!.SendException(e, eT);
}
public static void SendWarning(Exception e, ErrorType eT = ErrorType.Warning) =>
invoker!.SendException(e, eT);
public static void SendExceptionWithoutPage(Exception e, ErrorType eT = ErrorType.Unhandled)
{
SentryHelper.ExceptionHandler(e, eT == ErrorType.Unhandled ? SentryHelper.ExceptionType.UnhandledOther : SentryHelper.ExceptionType.Handled);
ExceptionContent = e!.ToString();
ExceptionType = eT;
SetPageTitle(eT);
Expand Down
2 changes: 2 additions & 0 deletions CollapseLauncher/Classes/Extension/UIElementExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System.Runtime.CompilerServices;
using Windows.UI;
using Windows.UI.Text;
using Hi3Helper.SentryHelper;

namespace CollapseLauncher.Extension
{
Expand Down Expand Up @@ -921,6 +922,7 @@ void AttachShadow(FrameworkElement thisElement, bool innerMask, Vector3? _offset
}
catch (Exception ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledXaml);
Logger.LogWriteLine($"Failed while attaching shadow to an element\r\n{ex}", LogType.Warning, true);
}
}
Expand Down
4 changes: 4 additions & 0 deletions CollapseLauncher/Classes/FileDialogCOM/FileDialogNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Hi3Helper.SentryHelper;

namespace CollapseLauncher.FileDialogCOM
{
Expand Down Expand Up @@ -79,6 +80,7 @@ private static ValueTask<object> GetPickerOpenTask<T>(object defaultValue, Dicti
}
catch (COMException ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
Logger.LogWriteLine($"COM Exception: {ex}", LogType.Error, true);
return new ValueTask<object>(defaultValue);
}
Expand Down Expand Up @@ -117,6 +119,7 @@ private static ValueTask<string> GetPickerSaveTask<T>(string defaultValue, Dicti
}
catch (COMException ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
Logger.LogWriteLine($"COM Exception: {ex}", LogType.Error, true);
return new ValueTask<string>(defaultValue);
}
Expand Down Expand Up @@ -187,6 +190,7 @@ private static string COMPtrToUnicodeString(IntPtr ptr)
}
catch (Exception e)
{
SentryHelper.ExceptionHandler(e, SentryHelper.ExceptionType.UnhandledOther);
Logger.LogWriteLine($"Error while marshalling COM Pointer {ptr} to string!\r\n{e}");
throw;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Hi3Helper.SentryHelper;
using static Hi3Helper.Logger;

namespace CollapseLauncher.GamePlaytime
Expand Down Expand Up @@ -125,6 +126,7 @@ public static CollapsePlaytime Load(RegistryKey root, int hashID,
}
catch (Exception ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"Failed while reading playtime.\r\n{ex}", LogType.Error, true);
}
finally
Expand Down Expand Up @@ -170,6 +172,7 @@ public void Save(bool forceUpdateDb = false)
}
catch (Exception ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"Failed to save playtime!\r\n{ex}", LogType.Error, true);
}
}
Expand Down Expand Up @@ -348,6 +351,7 @@ public void AddMinute()
}
catch (Exception ex)
{
await SentryHelper.ExceptionHandlerAsync(ex, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"[CollapsePlaytime::DbSync] Failed when trying to do sync operation\r\n{ex}",
LogType.Error, true);
return (false, null);
Expand All @@ -364,16 +368,18 @@ private async Task UpdatePlaytime_Database_Push(string jsonData, double totalTim
try
{
var unixStamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
await DbHandler.StoreKeyValue(KeyPlaytimeJson, jsonData);
await DbHandler.StoreKeyValue(KeyTotalTime, totalTime.ToString(CultureInfo.InvariantCulture));
await DbHandler.StoreKeyValue(KeyLastPlayed, lastPlayed != null ? lastPlayed.Value.ToString(CultureInfo.InvariantCulture) : "null");
await DbHandler.StoreKeyValue(KeyLastUpdated, unixStamp.ToString());
await DbHandler.StoreKeyValue(KeyPlaytimeJson, jsonData, true);
await DbHandler.StoreKeyValue(KeyTotalTime, totalTime.ToString(CultureInfo.InvariantCulture), true);
await DbHandler.StoreKeyValue(KeyLastPlayed, lastPlayed != null ? lastPlayed.Value.ToString(CultureInfo.InvariantCulture) : "null", true);
await DbHandler.StoreKeyValue(KeyLastUpdated, unixStamp.ToString(), true);
DbConfig.SetAndSaveValue(KeyLastUpdated, unixStamp);
_unixStampDb = Convert.ToInt32(unixStamp);
LastDbUpdate = curDateTime;
LogWriteLine("[CollapsePlaytime::UpdatePlaytime_Database_Push()] Successfully uploaded playtime data to database!", LogType.Scheme, true);
}
catch (Exception e)
{
await SentryHelper.ExceptionHandlerAsync(e, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"Failed when syncing Playtime to DB!\r\n{e}", LogType.Error, true);
}
finally
Expand All @@ -390,18 +396,20 @@ private async Task UpdatePlaytime_Database_Pull()
{
_isDbPulling = true;

_jsonDataDb = await DbHandler.QueryKey(KeyPlaytimeJson);
_jsonDataDb = await DbHandler.QueryKey(KeyPlaytimeJson, true);

var totalTimeDbStr = await DbHandler.QueryKey(KeyTotalTime);
var totalTimeDbStr = await DbHandler.QueryKey(KeyTotalTime, true);
_totalTimeDb = string.IsNullOrEmpty(totalTimeDbStr) ? null : Convert.ToDouble(totalTimeDbStr, CultureInfo.InvariantCulture);

var lpDb = await DbHandler.QueryKey(KeyLastPlayed);
var lpDb = await DbHandler.QueryKey(KeyLastPlayed, true);
_lastPlayedDb = !string.IsNullOrEmpty(lpDb) && !lpDb.Contains("null") ? Convert.ToDouble(lpDb, CultureInfo.InvariantCulture) : null; // if Db data is null, return null

_isDbPullSuccess = true;
LogWriteLine("[CollapsePlaytime::UpdatePlaytime_Database_Pull()] Successfully pulled data from database!", LogType.Scheme, true);
}
catch (Exception e)
{
await SentryHelper.ExceptionHandlerAsync(e, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"Failed when syncing Playtime to DB!\r\n{e}", LogType.Error, true);
}
finally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ internal class ImportExportBase
}
catch (Exception ex)
{
// Gets caught by the calling method
return ex;
}

Expand Down Expand Up @@ -190,6 +191,7 @@ private void ReadV3Values(Stream fs, string? gameBasePath)
}
catch (Exception ex)
{
// Gets caught by calling method
return ex;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;
using Hi3Helper.SentryHelper;

#nullable enable
namespace CollapseLauncher.GameSettings.Base
Expand Down Expand Up @@ -319,6 +320,7 @@ public static T LoadWithMagic(byte[] magic, SettingsGameVersionManager versionMa
}
catch (Exception ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
Logger.LogWriteLine($"Failed to parse MagicNodeBaseValues settings\r\n{ex}", LogType.Error, true);
return DefaultValue(magic, versionManager, typeInfo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(new Exception("Failed to save Genshin settings, please report them to GitHub issues!", ex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public override void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save Genshin Impact ScreenManager Values!\r\n{ex}", LogType.Error, true);
ErrorSender.SendException(new Exception("Failed to save Genshin Impact ScreenManager Values!", ex));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hi3Helper.EncTool;
using Microsoft.Win32;
using System;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -72,6 +73,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hi3Helper.EncTool;
using Microsoft.Win32;
using System;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -72,6 +73,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hi3Helper;
using Microsoft.Win32;
using System;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -76,6 +77,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System;
using System.Text;
using System.Text.Json.Serialization;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Data.ConverterTool;
using static Hi3Helper.Logger;
Expand Down Expand Up @@ -223,6 +224,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Win32;
using System;
using System.Text;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -115,6 +116,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using CollapseLauncher.Interfaces;
using Hi3Helper;
using Hi3Helper.EncTool;
using Hi3Helper.SentryHelper;
using Microsoft.Win32;
using System;
using System.Text;
Expand Down Expand Up @@ -224,6 +225,7 @@ public void Save()
}
catch (Exception ex)
{
SentryHelper.ExceptionHandler(ex, SentryHelper.ExceptionType.UnhandledOther);
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Hi3Helper;
using Microsoft.Win32;
using System;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -73,6 +74,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Drawing;
using System.Text;
using System.Text.Json.Serialization;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -141,6 +142,7 @@ public override void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Hi3Helper.EncTool;
using Microsoft.Win32;
using System;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -69,6 +70,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Microsoft.Win32;
using System;
using System.Text;
using Hi3Helper.SentryHelper;
using static CollapseLauncher.GameSettings.Base.SettingsBase;
using static Hi3Helper.Logger;

Expand Down Expand Up @@ -98,6 +99,7 @@ public void Save()
catch (Exception ex)
{
LogWriteLine($"Failed to save {_ValueName}!\r\n{ex}", LogType.Error, true);
SentryHelper.ExceptionHandler(new Exception($"Failed to save {_ValueName}!", ex), SentryHelper.ExceptionType.UnhandledOther);
}

}
Expand Down
Loading

0 comments on commit 7ac974b

Please sign in to comment.