Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 31 additions & 20 deletions Runtime/BacktraceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -930,25 +930,32 @@ internal void HandleLowMemory()
/// <param name="type">log type</param>
internal void HandleUnityMessage(string message, string stackTrace, LogType type)
{
if (!Enabled)
if (!Enabled || !Configuration.HandleUnhandledExceptions)
{
return;
}
var unityMessage = new BacktraceUnityMessage(message, stackTrace, type);
if (Configuration.HandleUnhandledExceptions && unityMessage.IsUnhandledException())
if (string.IsNullOrEmpty(message) || (type != LogType.Error && type != LogType.Exception))
{
BacktraceUnhandledException exception = null;
var invokeSkipApi = true;

// detect sampling flow
// we should apply sampling only to unhandled exceptions that are type LogType == Error
// log type error won't provide full exception information
if (type == LogType.Error && SamplingShouldSkip())
return;
}
BacktraceUnhandledException exception = null;
var invokeSkipApi = true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this ever be false? Who can modify this guy?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - if Sampling hit and SkipReport callback said "no, we shouldn't filter this report". In this case we don't need to invoke SkipCallback anymore in the ShouldSendReport method.

// detect sampling flow for LogType.Error + filter LogType.Error if client prefer to ignore them.
if (type == LogType.Error)
{
if (Configuration.ReportFilterType.HasFlag(ReportFilterType.Error))
{
return;
}
if (SamplingShouldSkip())
{
if (SkipReport != null || Configuration.ReportFilterType.HasFlag(ReportFilterType.UnhandledException))
if (SkipReport != null)
{
exception = new BacktraceUnhandledException(unityMessage.Message, unityMessage.StackTrace);
if (ShouldSkipReport(ReportFilterType.UnhandledException, exception, string.Empty))
exception = new BacktraceUnhandledException(message, stackTrace)
{
Type = type
};
if (ShouldSkipReport(ReportFilterType.Error, exception, string.Empty))
{
return;
}
Expand All @@ -959,14 +966,17 @@ internal void HandleUnityMessage(string message, string stackTrace, LogType type
return;
}
}
}

if (exception == null)
if (exception == null)
{
exception = new BacktraceUnhandledException(message, stackTrace)
{
exception = new BacktraceUnhandledException(unityMessage.Message, unityMessage.StackTrace);
}

SendUnhandledException(exception, invokeSkipApi);
Type = type
};
}

SendUnhandledException(exception, invokeSkipApi);
}

/// <summary>
Expand Down Expand Up @@ -1010,9 +1020,10 @@ private bool ShouldSendReport(Exception exception, List<string> attachmentPaths,
var filterType = ReportFilterType.Exception;
if (exception is BacktraceUnhandledException)
{
filterType = (exception as BacktraceUnhandledException).Classifier == "ANRException"
var unhandledException = (exception as BacktraceUnhandledException);
filterType = unhandledException.Classifier == "ANRException"
? ReportFilterType.Hang
: ReportFilterType.UnhandledException;
: unhandledException.Type == LogType.Exception ? ReportFilterType.UnhandledException: ReportFilterType.Error;
}


Expand Down
2 changes: 2 additions & 0 deletions Runtime/Model/BacktraceUnhandledException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace Backtrace.Unity.Model
{
Expand Down Expand Up @@ -38,6 +39,7 @@ public override string StackTrace
return _stacktrace;
}
}
public LogType Type { get; set; } = LogType.Exception;

/// <summary>
/// Unhandled exception stack frames
Expand Down
93 changes: 0 additions & 93 deletions Runtime/Model/BacktraceUnityMessage.cs

This file was deleted.

11 changes: 0 additions & 11 deletions Runtime/Model/BacktraceUnityMessage.cs.meta

This file was deleted.

11 changes: 10 additions & 1 deletion Runtime/Types/ReportFilterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ public enum ReportFilterType
/// Game hang
/// </summary>
[Tooltip("Game hang.")]
Hang = 8
Hang = 8,

/// <summary>
/// Unhandled exception - Error and Exception messages generated by Unity Logger.
/// </summary>
[Tooltip("Game error.")]
#if UNITY_2019_2_OR_NEWER
[InspectorName("Game error")]
#endif
Error = 16
}
}
53 changes: 53 additions & 0 deletions Tests/Runtime/ReportFilter/ReportFilterTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,59 @@ public void Setup()
AfterSetup(false);
}

[UnityTest]
public IEnumerator TestErrorTypeFilter_ShouldFilterErrorLog_ShouldPreventFromSendingDataToBacktrace()
{
const string errorMessage = "errorMessage";
var eventCalled = false;
BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string msg) =>
{
eventCalled = true;
return false;
};
BacktraceClient.Configuration.ReportFilterType = ReportFilterType.Error;

BacktraceClient.HandleUnityMessage(errorMessage, string.Empty, LogType.Error);
yield return new WaitForEndOfFrame();

Assert.IsFalse(eventCalled);
}

[Test]
public void TestErrorTypeFilter_ShouldntFilterErrorLogWhenFilterDoesntIncludeIt_ShouldInvokeSkipCallback()
{
const string errorMessage = "errorMessage";
var eventCalled = false;
BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string msg) =>
{
eventCalled = true;
return false;
};
BacktraceClient.Configuration.ReportFilterType = ReportFilterType.UnhandledException;

BacktraceClient.HandleUnityMessage(errorMessage, string.Empty, LogType.Error);

Assert.IsTrue(eventCalled);
}


[Test]
public void TestErrorTypeFilterShouldSetCorrectReportFilterType_ReportFilterTypeHasCorrectValue()
{
const string errorMessage = "errorMessage";
var reportFilterType = ReportFilterType.None;
BacktraceClient.SkipReport = (ReportFilterType type, Exception e, string msg) =>
{
reportFilterType = type;
return false;
};
BacktraceClient.Configuration.ReportFilterType = ReportFilterType.UnhandledException;

BacktraceClient.HandleUnityMessage(errorMessage, string.Empty, LogType.Error);

Assert.AreEqual(ReportFilterType.Error, reportFilterType);
}

[UnityTest]
public IEnumerator TestReportFilter_ShouldPreventFromSendingMessage_ClientNotSendingData()
{
Expand Down