Skip to content

Commit 60f586a

Browse files
authored
Merge pull request #2240 from DataDog/ncreated/GH-2239/limit-clear-all-data-errors
GH-2239 chore: Refine errors printed from `clearAllData()`
2 parents bac47c9 + 349c771 commit 60f586a

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

DatadogCore/Sources/Core/DataStore/FeatureDataStore.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ internal final class FeatureDataStore: DataStore {
9090
func clearAllData() {
9191
queue.async {
9292
do {
93-
let directory = try self.coreDirectory.coreDirectory.subdirectory(path: self.directoryPath)
94-
try directory.deleteAllFiles()
93+
let directory = try self.coreDirectory.coreDirectory.subdirectoryIfExists(path: self.directoryPath)
94+
try directory?.deleteAllFiles()
9595
} catch let error {
9696
DD.logger.error("[Data Store] Error on clearing all data for `\(self.feature)`", error: error)
9797
self.telemetry.error("[Data Store] Error on clearing all data for `\(self.feature)`", error: DDError(error: error))

DatadogCore/Sources/Core/Storage/Files/Directory.swift

+15
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,21 @@ internal struct Directory: DirectoryProtocol {
107107
}
108108
}
109109

110+
/// Returns directory at given path if it exists or `nil` otherwise. Throws if `path` exists but is not a directory.
111+
func subdirectoryIfExists(path: String) throws -> Directory? {
112+
let directoryURL = url.appendingPathComponent(path, isDirectory: true)
113+
var isDirectory = ObjCBool(false)
114+
let exists = FileManager.default.fileExists(atPath: directoryURL.path, isDirectory: &isDirectory)
115+
116+
guard exists else {
117+
return nil
118+
}
119+
guard isDirectory.boolValue else {
120+
throw InternalError(description: "Path is not a directory: \(directoryURL)")
121+
}
122+
return Directory(url: directoryURL)
123+
}
124+
110125
/// Creates file with given name.
111126
func createFile(named fileName: String) throws -> File {
112127
let fileURL = url.appendingPathComponent(fileName, isDirectory: false)

DatadogCore/Tests/Datadog/Core/Persistence/Files/DirectoryTests.swift

+17-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ class DirectoryTests: XCTestCase {
5555

5656
let uniqueName = uniqueSubdirectoryName()
5757
let expectedDirectory = try directory.createSubdirectory(path: uniqueName)
58-
let actualDirectory = try directory.subdirectory(path: uniqueName)
58+
let actualDirectory1 = try directory.subdirectory(path: uniqueName)
59+
let actualDirectory2 = try directory.subdirectoryIfExists(path: uniqueName)
5960

60-
XCTAssertEqual(expectedDirectory.url, actualDirectory.url)
61+
XCTAssertEqual(expectedDirectory.url, actualDirectory1.url)
62+
XCTAssertEqual(expectedDirectory.url, actualDirectory2?.url)
6163
}
6264

6365
func testItThrowsWhenAskedForSubdirectoryWhichDoesNotExist() throws {
@@ -75,6 +77,19 @@ class DirectoryTests: XCTestCase {
7577
}
7678
}
7779

80+
func testReturnsNilWhenAskedForSubdirectoryWhichDoesNotExist() throws {
81+
let directory = Directory(url: temporaryDirectory)
82+
CreateTemporaryDirectory()
83+
defer { DeleteTemporaryDirectory() }
84+
85+
XCTAssertNil(try directory.subdirectoryIfExists(path: "abc"))
86+
87+
_ = try directory.createFile(named: "file-instead-of-directory")
88+
XCTAssertThrowsError(try directory.subdirectory(path: "file-instead-of-directory")) { error in
89+
XCTAssertTrue(error is InternalError, "It should throw as given path is a file, not directory")
90+
}
91+
}
92+
7893
// MARK: - Files manipulation
7994

8095
func testItCreatesFile() throws {

0 commit comments

Comments
 (0)