Skip to content

Commit d4a2699

Browse files
authored
Ignore LogType.Error (#92)
* Ignore errors * Adjusted comment
1 parent aed7f18 commit d4a2699

File tree

6 files changed

+96
-125
lines changed

6 files changed

+96
-125
lines changed

Runtime/BacktraceClient.cs

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -930,25 +930,32 @@ internal void HandleLowMemory()
930930
/// <param name="type">log type</param>
931931
internal void HandleUnityMessage(string message, string stackTrace, LogType type)
932932
{
933-
if (!Enabled)
933+
if (!Enabled || !Configuration.HandleUnhandledExceptions)
934934
{
935935
return;
936936
}
937-
var unityMessage = new BacktraceUnityMessage(message, stackTrace, type);
938-
if (Configuration.HandleUnhandledExceptions && unityMessage.IsUnhandledException())
937+
if (string.IsNullOrEmpty(message) || (type != LogType.Error && type != LogType.Exception))
939938
{
940-
BacktraceUnhandledException exception = null;
941-
var invokeSkipApi = true;
942-
943-
// detect sampling flow
944-
// we should apply sampling only to unhandled exceptions that are type LogType == Error
945-
// log type error won't provide full exception information
946-
if (type == LogType.Error && SamplingShouldSkip())
939+
return;
940+
}
941+
BacktraceUnhandledException exception = null;
942+
var invokeSkipApi = true;
943+
// detect sampling flow for LogType.Error + filter LogType.Error if client prefer to ignore them.
944+
if (type == LogType.Error)
945+
{
946+
if (Configuration.ReportFilterType.HasFlag(ReportFilterType.Error))
947+
{
948+
return;
949+
}
950+
if (SamplingShouldSkip())
947951
{
948-
if (SkipReport != null || Configuration.ReportFilterType.HasFlag(ReportFilterType.UnhandledException))
952+
if (SkipReport != null)
949953
{
950-
exception = new BacktraceUnhandledException(unityMessage.Message, unityMessage.StackTrace);
951-
if (ShouldSkipReport(ReportFilterType.UnhandledException, exception, string.Empty))
954+
exception = new BacktraceUnhandledException(message, stackTrace)
955+
{
956+
Type = type
957+
};
958+
if (ShouldSkipReport(ReportFilterType.Error, exception, string.Empty))
952959
{
953960
return;
954961
}
@@ -959,14 +966,17 @@ internal void HandleUnityMessage(string message, string stackTrace, LogType type
959966
return;
960967
}
961968
}
969+
}
962970

963-
if (exception == null)
971+
if (exception == null)
972+
{
973+
exception = new BacktraceUnhandledException(message, stackTrace)
964974
{
965-
exception = new BacktraceUnhandledException(unityMessage.Message, unityMessage.StackTrace);
966-
}
967-
968-
SendUnhandledException(exception, invokeSkipApi);
975+
Type = type
976+
};
969977
}
978+
979+
SendUnhandledException(exception, invokeSkipApi);
970980
}
971981

972982
/// <summary>
@@ -1010,9 +1020,10 @@ private bool ShouldSendReport(Exception exception, List<string> attachmentPaths,
10101020
var filterType = ReportFilterType.Exception;
10111021
if (exception is BacktraceUnhandledException)
10121022
{
1013-
filterType = (exception as BacktraceUnhandledException).Classifier == "ANRException"
1023+
var unhandledException = (exception as BacktraceUnhandledException);
1024+
filterType = unhandledException.Classifier == "ANRException"
10141025
? ReportFilterType.Hang
1015-
: ReportFilterType.UnhandledException;
1026+
: unhandledException.Type == LogType.Exception ? ReportFilterType.UnhandledException: ReportFilterType.Error;
10161027
}
10171028

10181029

Runtime/Model/BacktraceUnhandledException.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using UnityEngine;
45

56
namespace Backtrace.Unity.Model
67
{
@@ -38,6 +39,7 @@ public override string StackTrace
3839
return _stacktrace;
3940
}
4041
}
42+
public LogType Type { get; set; } = LogType.Exception;
4143

4244
/// <summary>
4345
/// Unhandled exception stack frames

Runtime/Model/BacktraceUnityMessage.cs

Lines changed: 0 additions & 93 deletions
This file was deleted.

Runtime/Model/BacktraceUnityMessage.cs.meta

Lines changed: 0 additions & 11 deletions
This file was deleted.

Runtime/Types/ReportFilterType.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ public enum ReportFilterType
4444
/// Game hang
4545
/// </summary>
4646
[Tooltip("Game hang.")]
47-
Hang = 8
47+
Hang = 8,
48+
49+
/// <summary>
50+
/// Unhandled exception - Error and Exception messages generated by Unity Logger.
51+
/// </summary>
52+
[Tooltip("Game error.")]
53+
#if UNITY_2019_2_OR_NEWER
54+
[InspectorName("Game error")]
55+
#endif
56+
Error = 16
4857
}
4958
}

Tests/Runtime/ReportFilter/ReportFilterTypeTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,59 @@ public void Setup()
1919
AfterSetup(false);
2020
}
2121

22+
[UnityTest]
23+
public IEnumerator TestErrorTypeFilter_ShouldFilterErrorLog_ShouldPreventFromSendingDataToBacktrace()
24+
{
25+
const string errorMessage = "errorMessage";
26+
var eventCalled = false;
27+
BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string msg) =>
28+
{
29+
eventCalled = true;
30+
return false;
31+
};
32+
BacktraceClient.Configuration.ReportFilterType = ReportFilterType.Error;
33+
34+
BacktraceClient.HandleUnityMessage(errorMessage, string.Empty, LogType.Error);
35+
yield return new WaitForEndOfFrame();
36+
37+
Assert.IsFalse(eventCalled);
38+
}
39+
40+
[Test]
41+
public void TestErrorTypeFilter_ShouldntFilterErrorLogWhenFilterDoesntIncludeIt_ShouldInvokeSkipCallback()
42+
{
43+
const string errorMessage = "errorMessage";
44+
var eventCalled = false;
45+
BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string msg) =>
46+
{
47+
eventCalled = true;
48+
return false;
49+
};
50+
BacktraceClient.Configuration.ReportFilterType = ReportFilterType.UnhandledException;
51+
52+
BacktraceClient.HandleUnityMessage(errorMessage, string.Empty, LogType.Error);
53+
54+
Assert.IsTrue(eventCalled);
55+
}
56+
57+
58+
[Test]
59+
public void TestErrorTypeFilterShouldSetCorrectReportFilterType_ReportFilterTypeHasCorrectValue()
60+
{
61+
const string errorMessage = "errorMessage";
62+
var reportFilterType = ReportFilterType.None;
63+
BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string msg) =>
64+
{
65+
reportFilterType = type;
66+
return false;
67+
};
68+
BacktraceClient.Configuration.ReportFilterType = ReportFilterType.UnhandledException;
69+
70+
BacktraceClient.HandleUnityMessage(errorMessage, string.Empty, LogType.Error);
71+
72+
Assert.AreEqual(ReportFilterType.Error, reportFilterType);
73+
}
74+
2275
[UnityTest]
2376
public IEnumerator TestReportFilter_ShouldPreventFromSendingMessage_ClientNotSendingData()
2477
{

0 commit comments

Comments
 (0)