Skip to content

Commit

Permalink
fix: FileManager log info when path doesn't exist (#3594)
Browse files Browse the repository at this point in the history
Logging an error when getting all files for a folder that doesn't exist
confuses users; see GH-3577. Instead, log an info message that the
SentryFileManager tried getting a list of files for a folder that
doesn't exist. We chose info because it's still an edge case that
shouldn't occur often, but it doesn't break any functionality. If a path
was deleted manually by the user that the SDK requires, the SDK will
create the path on next launch. We don't want to add checks if all the
necessary paths exist every time the SDK stores an envelope cause this
adds some overhead. If users manually remove the required paths, we
accept that they have to wait until the next time the SDK launches.
  • Loading branch information
philipphofmann authored Jan 31, 2024
1 parent e003030 commit 86f7249
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- Clarify FramesTracker log message (#3570)
- Fix rare battery breadcrumbs crash (#3582)
- Fix synchronization issue in FramesTracker (#3571)
- Fix FileManager logs info instead of error when a path doesn't exist (#3594)

## 8.19.0

Expand Down
9 changes: 7 additions & 2 deletions Sources/Sentry/SentryFileManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,16 @@ - (void)deleteAllEnvelopes
- (NSArray<NSString *> *)allFilesInFolder:(NSString *)path
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:path]) {
SENTRY_LOG_INFO(@"Returning empty files list, as folder doesn't exist at path: %@", path);
return @[];
}

NSError *error = nil;
NSArray<NSString *> *storedFiles = [fileManager contentsOfDirectoryAtPath:path error:&error];
if (nil != error) {
if (error != nil) {
SENTRY_LOG_ERROR(@"Couldn't load files in folder %@: %@", path, error);
return [NSArray new];
return @[];
}
return [storedFiles sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
}
Expand Down
17 changes: 17 additions & 0 deletions Tests/SentryTests/Helper/SentryFileManagerTests.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Nimble
import Sentry
import SentryTestUtils
import XCTest
Expand Down Expand Up @@ -473,6 +474,22 @@ class SentryFileManagerTests: XCTestCase {
XCTAssertEqual(0, sut.getAllEnvelopes().count)
}

func testGetAllEnvelopesWhenNoEnvelopesPath_LogsInfoMessage() {
let logOutput = TestLogOutput()
SentryLog.setLogOutput(logOutput)
SentryLog.configure(true, diagnosticLevel: .debug)

sut.deleteAllFolders()
sut.getAllEnvelopes()

let debugLogMessages = logOutput.loggedMessages.filter { $0.contains("[Sentry] [info]") && $0.contains("Returning empty files list, as folder doesn't exist at path:") }
expect(debugLogMessages.count) == 1

let errorMessages = logOutput.loggedMessages.filter { $0.contains("[Sentry] [error]") }

expect(errorMessages.count) == 0
}

func testReadStoreDeleteAppState() {
sut.store(TestData.appState)

Expand Down

0 comments on commit 86f7249

Please sign in to comment.