Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Marking Unity exceptions as unhandled by default #542

Merged
merged 8 commits into from
Feb 4, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Features

- Unity logged exceptions are marked as unhandled by default ([#542](https://github.com/getsentry/sentry-unity/pull/542))

### Fixes

- Importing the link.xml when opening the config window no longer causes an infinite loop ([#539](https://github.com/getsentry/sentry-unity/pull/539))
Expand Down
4 changes: 2 additions & 2 deletions samples/unity-of-bugs/Assets/Scripts/BugFarmButtons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void ThrowExceptionAndCatch()
}
catch (Exception e)
{
Debug.LogException(e);
SentrySdk.CaptureException(e);
}
}

Expand All @@ -46,7 +46,7 @@ public void ThrowNullAndCatch()
}
catch (Exception e)
{
Debug.LogException(e);
SentrySdk.CaptureException(e);
}
}

Expand Down
11 changes: 8 additions & 3 deletions src/Sentry.Unity/UnityLogException.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using Sentry.Internal;
using Sentry.Protocol;
using UnityEngine;

Expand All @@ -21,10 +22,9 @@ public UnityLogException(string logString, string logStackTrace)
{
LogString = logString;
LogStackTrace = logStackTrace;
Data[Mechanism.MechanismKey] = "unity.log";
}

private UnityLogException() : base()
internal UnityLogException() : base()
{
LogString = "";
LogStackTrace = "";
Expand Down Expand Up @@ -140,7 +140,12 @@ public SentryException ToSentryException()
{
Stacktrace = stacktrace,
Type = excType,
Value = excValue
Value = excValue,
Mechanism = new Mechanism
{
Handled = false,
Type = "unity.log"
}
};
}

Expand Down
22 changes: 22 additions & 0 deletions test/Sentry.Unity.Tests/IntegrationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,28 @@ public IEnumerator BugFarmScene_MultipleSentryInit_SendEventForTheLatest()
Assert.AreEqual(1, nextEventCapture.Events.Count);
}

[UnityTest]
public IEnumerator BugFarmScene_DebugLogException_IsMarkedUnhandled()
{
yield return SetupSceneCoroutine("1_BugFarm");

// arrange
var testEventCapture = new TestEventCapture();
using var _ = InitSentrySdk(o =>
{
o.AddIntegration(new UnityApplicationLoggingIntegration(eventCapture: testEventCapture));
});
var testBehaviour = new GameObject("TestHolder").AddComponent<TestMonoBehaviour>();

// act
testBehaviour.gameObject.SendMessage(nameof(testBehaviour.DebugLogException));

// assert
Assert.NotNull(testEventCapture.Events.SingleOrDefault(sentryEvent =>
sentryEvent.SentryExceptions.SingleOrDefault(exception =>
exception.Mechanism?.Handled is false) is not null));
}

[UnityTest]
public IEnumerator Init_OptionsAreDefaulted()
{
Expand Down
3 changes: 3 additions & 0 deletions test/Sentry.Unity.Tests/TestBehaviours/TestMonoBehaviour.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@ public void TestException()

public void DebugLogError()
=> Debug.LogError("error");

public void DebugLogException()
=> Debug.LogException(new Exception("Unity log exception"));
}
}
18 changes: 18 additions & 0 deletions test/Sentry.Unity.Tests/UnityLogExceptionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ namespace Sentry.Unity.Tests
{
public class UnityLogExceptionTests
{
[Test]
public void ToSentryException_MarkedAsUnhandled()
{
var sentryException = new UnityLogException().ToSentryException();

Assert.IsFalse(sentryException.Mechanism?.Handled);
}

[TestCaseSource(nameof(ParsingTestCases))]
public void ToSentryException_ParsingTestCases(
string logString,
Expand Down Expand Up @@ -103,6 +111,11 @@ public void ToSentryException_ParsingTestCases(
InApp = true
},
}
},
Mechanism = new Mechanism
{
Handled = false,
Type = "unity.log"
}
} },
new object[] {
Expand Down Expand Up @@ -236,6 +249,11 @@ public void ToSentryException_ParsingTestCases(
InApp = false
},
}
},
Mechanism = new Mechanism
{
Handled = false,
Type = "unity.log"
}
} },
// TODO: Current parsing logic isn't able to parse this (editor only stack trace)
Expand Down