diff --git a/src/Sentry/GlobalSessionManager.cs b/src/Sentry/GlobalSessionManager.cs index 8fb6cb8413..6951fb6293 100644 --- a/src/Sentry/GlobalSessionManager.cs +++ b/src/Sentry/GlobalSessionManager.cs @@ -151,6 +151,12 @@ private void DeletePersistedSession() } var filePath = Path.Combine(_persistenceDirectoryPath, PersistedSessionFileName); + if (!_options.FileSystem.FileExists(filePath)) + { + _options.LogDebug("A persisted session file was not found at '{0}'.", filePath); + return null; + } + try { var recoveredUpdate = _persistedSessionProvider(filePath); @@ -195,12 +201,6 @@ private void DeletePersistedSession() return sessionUpdate; } - catch (Exception ex) when (ex is FileNotFoundException or DirectoryNotFoundException) - { - // Not a notable error - _options.LogDebug("A persisted session does not exist ({0}) at {1}.", ex.GetType().Name, filePath); - return null; - } catch (Exception ex) { _options.LogError(ex, "Failed to recover persisted session from the file system '{0}'.", filePath); diff --git a/test/Sentry.Tests/GlobalSessionManagerTests.cs b/test/Sentry.Tests/GlobalSessionManagerTests.cs index 2649319a4f..7365fa1dac 100644 --- a/test/Sentry.Tests/GlobalSessionManagerTests.cs +++ b/test/Sentry.Tests/GlobalSessionManagerTests.cs @@ -261,45 +261,28 @@ public void TryRecoverPersistedSession_SessionNotStarted_ReturnsNull() } [Fact] - public void TryRecoverPersistedSession_FileNotFoundException_LogDebug() + public void TryRecoverPersistedSession_NoSessionFile_LogDebug() { // Arrange + _fixture.PersistedSessionProvider = _ => throw new UnreachableException("Unexpected attempt to read a file that does not exist."); var sut = _fixture.GetSut(); - sut = new GlobalSessionManager( - _fixture.Options, - persistedSessionProvider: _ => throw new FileNotFoundException()); // Act sut.TryRecoverPersistedSession(); // Assert - _fixture.Logger.Entries.Should().Contain(e => e.Level == SentryLevel.Debug); - } - - [Fact] - public void TryRecoverPersistedSession_DirectoryNotFoundException_LogDebug() - { - // Arrange - var sut = _fixture.GetSut(); - sut = new GlobalSessionManager( - _fixture.Options, - persistedSessionProvider: _ => throw new DirectoryNotFoundException()); - - // Act - sut.TryRecoverPersistedSession(); - - // Assert - _fixture.Logger.Entries.Should().Contain(e => e.Level == SentryLevel.Debug); + _fixture.Logger.Entries.Should().Contain(e => + e.Level == SentryLevel.Debug + && e.Message.Contains("A persisted session file was not found")); } [Fact] public void TryRecoverPersistedSession_EndOfStreamException_LogError() { // Arrange + _fixture.PersistedSessionProvider = _ => throw new EndOfStreamException(); var sut = _fixture.GetSut(); - sut = new GlobalSessionManager( - _fixture.Options, - persistedSessionProvider: _ => throw new EndOfStreamException()); + sut.StartSession(); // Act sut.TryRecoverPersistedSession(); @@ -391,6 +374,7 @@ public void TryRecoverPersistedSession_CrashDelegateReturnsTrueWithPauseTimestam pausedTimestamp); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -412,6 +396,7 @@ public void TryRecoverPersistedSession_CrashDelegateIsNullWithPauseTimestamp_End pausedTimestamp); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -568,6 +553,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndNoCrash_EndsAsUnha pendingUnhandled: true); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -588,6 +574,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndCrash_EscalatesToC pendingUnhandled: true); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession(); @@ -609,6 +596,7 @@ public void TryRecoverPersistedSession_WithPendingUnhandledAndPauseTimestamp_Esc pendingUnhandled: true); var sut = _fixture.GetSut(); + sut.StartSession(); // Act var persistedSessionUpdate = sut.TryRecoverPersistedSession();