From 83bad812c6620f120203083fcb3fddeda837895a Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Fri, 26 Jan 2024 15:40:13 +0100 Subject: [PATCH 1/3] Handle app domain exception if script call optimization is enabled --- Runtime/BacktraceClient.cs | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/Runtime/BacktraceClient.cs b/Runtime/BacktraceClient.cs index 788657b2..ff69058f 100644 --- a/Runtime/BacktraceClient.cs +++ b/Runtime/BacktraceClient.cs @@ -510,6 +510,13 @@ public void Refresh() #endif _current = Thread.CurrentThread; CaptureUnityMessages(); + if (Configuration.HandleUnhandledExceptions) + { + AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; +#if UNITY_ANDROID || UNITY_IOS + Application.lowMemory += HandleLowMemory; +#endif + } _reportLimitWatcher = new ReportLimitWatcher(Convert.ToUInt32(Configuration.ReportPerMin)); _clientReportAttachments = Configuration.GetAttachmentPaths(); @@ -1014,14 +1021,9 @@ internal void HandleUnhandledExceptionsFromAndroidBackgroundThread(string backgr private void CaptureUnityMessages() { _backtraceLogManager = new BacktraceLogManager(Configuration.NumberOfLogs); - if (Configuration.HandleUnhandledExceptions) - { - Application.logMessageReceived += HandleUnityMessage; - Application.logMessageReceivedThreaded += HandleUnityBackgroundException; -#if UNITY_ANDROID || UNITY_IOS - Application.lowMemory += HandleLowMemory; -#endif - } + Application.logMessageReceived += HandleUnityMessage; + Application.logMessageReceivedThreaded += HandleUnityBackgroundException; + } internal void OnApplicationPause(bool pause) @@ -1283,6 +1285,17 @@ private void HandleInnerException(BacktraceReport report) } } + /// + /// Handle application domain exception that could happen + /// when the "Script call optimization" option is enabled. + /// + private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) + { + var exception = e.ExceptionObject as Exception; + SendUnhandledException(exception); + } + + /// /// Validate if current client configuration is valid /// From 4d4a05b32c260253020addf2c522b85ef5eb6c87 Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Fri, 26 Jan 2024 16:51:45 +0100 Subject: [PATCH 2/3] Use Exception rather than UnhandledException type --- Runtime/BacktraceClient.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Runtime/BacktraceClient.cs b/Runtime/BacktraceClient.cs index ff69058f..30524456 100644 --- a/Runtime/BacktraceClient.cs +++ b/Runtime/BacktraceClient.cs @@ -1144,7 +1144,7 @@ private bool SamplingShouldSkip() return value > Configuration.Sampling; } - private void SendUnhandledException(BacktraceUnhandledException exception, bool invokeSkipApi = true) + private void SendUnhandledException(Exception exception, bool invokeSkipApi = true) { if (OnUnhandledApplicationException != null) { From 3249c581cd09db08eb07eac3b72d47c8d75285bc Mon Sep 17 00:00:00 2001 From: Konrad Dysput Date: Mon, 29 Jan 2024 15:28:23 +0100 Subject: [PATCH 3/3] Report Unhandled exceptions from AppDomain --- Runtime/BacktraceClient.cs | 21 ++++++++++++++++++++- Runtime/Model/BacktraceReport.cs | 5 ++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Runtime/BacktraceClient.cs b/Runtime/BacktraceClient.cs index 30524456..1a6a186a 100644 --- a/Runtime/BacktraceClient.cs +++ b/Runtime/BacktraceClient.cs @@ -706,6 +706,7 @@ private void OnDestroy() _instance = null; Application.logMessageReceived -= HandleUnityMessage; Application.logMessageReceivedThreaded -= HandleUnityBackgroundException; + AppDomain.CurrentDomain.UnhandledException -= CurrentDomain_UnhandledException; #if UNITY_ANDROID || UNITY_IOS Application.lowMemory -= HandleLowMemory; #endif @@ -1292,7 +1293,25 @@ private void HandleInnerException(BacktraceReport report) private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { var exception = e.ExceptionObject as Exception; - SendUnhandledException(exception); + if (OnUnhandledApplicationException != null) + { + OnUnhandledApplicationException.Invoke(exception); + } + + if (ShouldSkipReport(ReportFilterType.UnhandledException, exception, string.Empty)) + { + return; + } + + if (Database == null) + { + SendUnhandledException(exception); + return; + } + var report = new BacktraceReport(exception, new Dictionary() { + { "error.type", BacktraceDefaultClassifierTypes.UnhandledExceptionType } + }); + Database.Add(SetupBacktraceData(report)); } diff --git a/Runtime/Model/BacktraceReport.cs b/Runtime/Model/BacktraceReport.cs index 88c3e00e..c2b1b443 100644 --- a/Runtime/Model/BacktraceReport.cs +++ b/Runtime/Model/BacktraceReport.cs @@ -188,7 +188,10 @@ private void SetClassifierInfo() } else { - Attributes[ErrorTypeAttributeName] = BacktraceDefaultClassifierTypes.ExceptionType; + if (!Attributes.ContainsKey(ErrorTypeAttributeName)) + { + Attributes[ErrorTypeAttributeName] = BacktraceDefaultClassifierTypes.ExceptionType; + } Classifier = Exception.GetType().Name; } }