Skip to content

Commit 006a67e

Browse files
authored
feat: Added GetLastRunState to the SentryUnity (#2049)
1 parent 0558392 commit 006a67e

File tree

4 files changed

+127
-2
lines changed

4 files changed

+127
-2
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Features
6+
7+
- Added `SentryUnity.CrashedLastRun()`. This allows you to check whether the SDK captured a crash the last time the game ran. ([#2049](https://github.com/getsentry/sentry-unity/pull/2049))
8+
59
### Fixes
610

711
- Fixed a potential race condition in the ANR watchdog integration ([2045](https://github.com/getsentry/sentry-unity/pull/2045))

src/Sentry.Unity/SentryUnity.cs

+38
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,42 @@ public static void Close()
4747
UnitySdk?.Close();
4848
UnitySdk = null;
4949
}
50+
51+
/// <summary>
52+
/// Represents the crash state of the games's previous run.
53+
/// Used to determine if the last execution terminated normally or crashed.
54+
/// </summary>
55+
public enum CrashedLastRun
56+
{
57+
/// <summary>
58+
/// The LastRunState is unknown. This might be due to the SDK not being initialized, native crash support
59+
/// missing, or being disabled.
60+
/// </summary>
61+
Unknown,
62+
63+
/// <summary>
64+
/// The application did not crash during the last run.
65+
/// </summary>
66+
DidNotCrash,
67+
68+
/// <summary>
69+
/// The application crashed during the last run.
70+
/// </summary>
71+
Crashed
72+
}
73+
74+
/// <summary>
75+
/// Retrieves the crash state of the previous application run.
76+
/// This indicates whether the application terminated normally or crashed.
77+
/// </summary>
78+
/// <returns><see cref="CrashedLastRun"/> indicating the state of the previous run.</returns>
79+
public static CrashedLastRun GetLastRunState()
80+
{
81+
if (UnitySdk is null)
82+
{
83+
return CrashedLastRun.Unknown;
84+
}
85+
86+
return UnitySdk.CrashedLastRun();
87+
}
5088
}

src/Sentry.Unity/SentryUnitySDK.cs

+14
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,18 @@ public void Close()
9999
"Exception while releasing the lockfile on the config directory.", ex);
100100
}
101101
}
102+
103+
public SentryUnity.CrashedLastRun CrashedLastRun()
104+
{
105+
if (_options.CrashedLastRun is null)
106+
{
107+
_options.DiagnosticLogger?.LogDebug("The SDK does not have a 'CrashedLastRun' set. " +
108+
"This might be due to a missing or disabled native integration.");
109+
return SentryUnity.CrashedLastRun.Unknown;
110+
}
111+
112+
return _options.CrashedLastRun.Invoke()
113+
? SentryUnity.CrashedLastRun.Crashed
114+
: SentryUnity.CrashedLastRun.DidNotCrash;
115+
}
102116
}

test/Sentry.Unity.Tests/SentryUnityTests.cs

+71-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ namespace Sentry.Unity.Tests;
1111

1212
public class SentryUnitySelfInitializationTests
1313
{
14+
private const string TestDsn = "https://[email protected]/4504604988538880";
15+
1416
[TearDown]
1517
public void TearDown()
1618
{
@@ -67,7 +69,7 @@ public void SentryUnity_OptionsValid_Initializes()
6769
{
6870
var options = new SentryUnityOptions
6971
{
70-
Dsn = "https://[email protected]/4504604988538880"
72+
Dsn = TestDsn
7173
};
7274

7375
SentryUnity.Init(options);
@@ -93,7 +95,7 @@ public void Init_MultipleTimes_LogsWarning()
9395
var options = new SentryUnityOptions
9496
{
9597
Debug = true,
96-
Dsn = "https://[email protected]/4504604988538880",
98+
Dsn = TestDsn,
9799
DiagnosticLogger = testLogger,
98100
};
99101

@@ -104,4 +106,71 @@ public void Init_MultipleTimes_LogsWarning()
104106
log.logLevel == SentryLevel.Warning &&
105107
log.message.Contains("The SDK has already been initialized.")));
106108
}
109+
110+
[Test]
111+
public void GetLastRunState_WithoutInit_ReturnsUnknown()
112+
{
113+
// Make sure SDK is closed
114+
SentryUnity.Close();
115+
116+
// Act
117+
var result = SentryUnity.GetLastRunState();
118+
119+
// Assert
120+
Assert.AreEqual(SentryUnity.CrashedLastRun.Unknown, result);
121+
}
122+
123+
[Test]
124+
public void GetLastRunState_WhenCrashed_ReturnsCrashed()
125+
{
126+
// Arrange
127+
var options = new SentryUnityOptions
128+
{
129+
Dsn = TestDsn,
130+
CrashedLastRun = () => true // Mock crashed state
131+
};
132+
133+
// Act
134+
SentryUnity.Init(options);
135+
var result = SentryUnity.GetLastRunState();
136+
137+
// Assert
138+
Assert.AreEqual(SentryUnity.CrashedLastRun.Crashed, result);
139+
}
140+
141+
[Test]
142+
public void GetLastRunState_WhenNotCrashed_ReturnsDidNotCrash()
143+
{
144+
// Arrange
145+
var options = new SentryUnityOptions
146+
{
147+
Dsn = TestDsn,
148+
CrashedLastRun = () => false // Mock non-crashed state
149+
};
150+
151+
// Act
152+
SentryUnity.Init(options);
153+
var result = SentryUnity.GetLastRunState();
154+
155+
// Assert
156+
Assert.AreEqual(SentryUnity.CrashedLastRun.DidNotCrash, result);
157+
}
158+
159+
[Test]
160+
public void GetLastRunState_WithNullDelegate_ReturnsUnknown()
161+
{
162+
// Arrange
163+
var options = new SentryUnityOptions
164+
{
165+
Dsn = TestDsn,
166+
CrashedLastRun = null // Explicitly set to null
167+
};
168+
169+
// Act
170+
SentryUnity.Init(options);
171+
var result = SentryUnity.GetLastRunState();
172+
173+
// Assert
174+
Assert.AreEqual(SentryUnity.CrashedLastRun.Unknown, result);
175+
}
107176
}

0 commit comments

Comments
 (0)