Browse Source

Implement a helper method to send info about exceptions that are safe to catch while also being caused by bugs

Equbuxu 2 years ago
parent
commit
17bdf8f4e5

+ 34 - 0
src/PixiEditor/Helpers/CrashHelper.cs

@@ -1,4 +1,7 @@
 using System.Globalization;
+using System.IO;
+using System.Net.Http;
+using System.Runtime.CompilerServices;
 using System.Text;
 using ByteSizeLib;
 using Hardware.Info;
@@ -97,4 +100,35 @@ internal class CrashHelper
             }
         }
     }
+
+    public static async Task SendExceptionInfoToWebhook(Exception e, [CallerFilePath] string filePath = "<unknown>", [CallerMemberName] string memberName = "<unknown>")
+    {
+        if (ViewModelMain.Current.DebugSubViewModel.IsDebugBuild)
+            return;
+        await SendReportTextToWebhook(CrashReport.Generate(e), $"{filePath}; Method {memberName}");
+    }
+
+    public static async Task SendReportTextToWebhook(CrashReport report, string catchLocation = null)
+    {
+        string reportText = report.ReportText;
+        if (catchLocation is not null)
+        {
+            reportText = $"The report was generated from an exception caught in {catchLocation}.\r\n{reportText}";
+        }
+
+        byte[] bytes = Encoding.UTF8.GetBytes(reportText);
+        string filename = Path.GetFileNameWithoutExtension(report.FilePath) + ".txt";
+
+        MultipartFormDataContent formData = new MultipartFormDataContent
+        {
+            { new ByteArrayContent(bytes, 0, bytes.Length), "crash-report", filename }
+        };
+        try
+        {
+            using HttpClient httpClient = new HttpClient();
+            string url = BuildConstants.CrashReportWebhookUrl;
+            await httpClient.PostAsync(url, formData);
+        }
+        catch { }
+    }
 }

+ 1 - 19
src/PixiEditor/ViewModels/CrashReportViewModel.cs

@@ -40,25 +40,7 @@ internal class CrashReportViewModel : ViewModelBase
         AttachDebuggerCommand = new(AttachDebugger);
 
         if (!IsDebugBuild)
-            SendReportTextToWebhook(report);
-    }
-
-    private async void SendReportTextToWebhook(CrashReport report)
-    {
-        byte[] bytes = Encoding.UTF8.GetBytes(report.ReportText);
-        string filename = Path.GetFileNameWithoutExtension(report.FilePath) + ".txt";
-
-        MultipartFormDataContent formData = new MultipartFormDataContent
-        {
-            { new ByteArrayContent(bytes, 0, bytes.Length), "crash-report", filename }
-        };
-        try
-        {
-            using HttpClient httpClient = new HttpClient();
-            string url = BuildConstants.CrashReportWebhookUrl;
-            await httpClient.PostAsync(url, formData);
-        }
-        catch { }
+            _ = CrashHelper.SendReportTextToWebhook(report);
     }
 
     public void RecoverDocuments(object args)

+ 2 - 1
src/PixiEditor/ViewModels/SubViewModels/Main/MiscViewModel.cs

@@ -26,9 +26,10 @@ internal class MiscViewModel : SubViewModel<ViewModelMain>
         {
             ProcessHelpers.ShellExecute(url);
         }
-        catch
+        catch (Exception e)
         {
             NoticeDialog.Show(title: "Error", message: $"Couldn't open the address {url} in your default browser");
+            _ = CrashHelper.SendExceptionInfoToWebhook(e);
         }
     }
 }