Skip to content

Commit a5692e6

Browse files
committed
catch all relevant errors from trys and log
1 parent e3cff2d commit a5692e6

File tree

5 files changed

+38
-17
lines changed

5 files changed

+38
-17
lines changed

Diff for: LlamaChat/model/chat/MessagesModel.swift

+8-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ class MessagesModel: ObservableObject {
1414
}()
1515

1616
private lazy var db: Connection? = {
17-
return databaseURL.flatMap { try? Connection($0.path) }
17+
return databaseURL.flatMap {
18+
do {
19+
return try Connection($0.path)
20+
} catch {
21+
print("Error getting DB connection:", error)
22+
return nil
23+
}
24+
}
1825
}()
1926

2027
private let chatSourcesTable = Table("chat_sources")

Diff for: LlamaChat/model/sources/ChatSources.swift

+10-3
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,10 @@ class ChatSources: ObservableObject {
119119
}
120120

121121
private func loadSources() {
122-
guard let persistedURL else { return }
122+
guard
123+
let persistedURL,
124+
FileManager.default.fileExists(atPath: persistedURL.path)
125+
else { return }
123126

124127
do {
125128
let jsonData = try Data(contentsOf: persistedURL)
@@ -138,7 +141,11 @@ class ChatSources: ObservableObject {
138141
guard let persistedURL else { return }
139142

140143
let jsonEncoder = JSONEncoder()
141-
let json = try? jsonEncoder.encode(Payload(sources: sources))
142-
try? json?.write(to: persistedURL)
144+
do {
145+
let json = try jsonEncoder.encode(Payload(sources: sources))
146+
try json.write(to: persistedURL)
147+
} catch {
148+
print("Error persisting sources:", error)
149+
}
143150
}
144151
}

Diff for: LlamaChat/model/sources/SourceNameGenerator.swift

+8-5
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ fileprivate struct Names: Decodable {
1414

1515
class SourceNameGenerator {
1616
private lazy var names: Names? = {
17-
guard
18-
let fileURL = Bundle.main.url(forResource: "names", withExtension: "json"),
19-
let data = try? Data(contentsOf: fileURL)
20-
else { return nil }
17+
guard let fileURL = Bundle.main.url(forResource: "names", withExtension: "json") else { return nil }
2118

22-
return try? JSONDecoder().decode(Names.self, from: data)
19+
do {
20+
let data = try Data(contentsOf: fileURL)
21+
return try JSONDecoder().decode(Names.self, from: data)
22+
} catch {
23+
print("Error loading source names:", error)
24+
return nil
25+
}
2326
}()
2427

2528
var canGenerateNames: Bool {

Diff for: LlamaChat/util/FileUtils.swift

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@
88
import Foundation
99

1010
func applicationSupportDirectoryURL() -> URL? {
11-
guard
12-
let bundleIdentifier = Bundle.main.bundleIdentifier,
13-
let url = try? FileManager().url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
14-
else { return nil }
11+
guard let bundleIdentifier = Bundle.main.bundleIdentifier else { return nil }
1512

16-
let appScopedDirectory = url.appendingPathComponent(bundleIdentifier, isDirectory: true)
1713
do {
14+
let url = try FileManager().url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
15+
let appScopedDirectory = url.appendingPathComponent(bundleIdentifier, isDirectory: true)
16+
1817
if !FileManager.default.fileExists(atPath: appScopedDirectory.path) {
1918
try FileManager.default.createDirectory(at: appScopedDirectory, withIntermediateDirectories: false)
2019
}
2120
return appScopedDirectory
2221
} catch {
22+
print("Error getting application support directory:", error)
2323
return nil
2424
}
2525
}

Diff for: LlamaChat/viewmodel/sources/configure/settings/ConfigureLocalPyTorchModelSettingsViewModel.swift

+7-3
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,13 @@ class ConfigureLocalPyTorchModelSettingsViewModel: ObservableObject, ConfigureLo
145145

146146
conversionState = .loading
147147
Task.init {
148-
let canConvert = (try? await ModelConverter().canRunConversion()) ?? false
149-
await MainActor.run {
150-
conversionState = .canConvert(canConvert)
148+
do {
149+
let canConvert = (try await ModelConverter().canRunConversion())
150+
await MainActor.run {
151+
conversionState = .canConvert(canConvert)
152+
}
153+
} catch {
154+
print("Error determining model conversion status", error)
151155
}
152156
}
153157
}

0 commit comments

Comments
 (0)