Skip to content

Commit 2192579

Browse files
authored
Don't crash the app if the LCP database cannot be opened (#528)
1 parent 55694d6 commit 2192579

File tree

4 files changed

+29
-31
lines changed

4 files changed

+29
-31
lines changed

Sources/Adapters/LCPSQLite/Database.swift

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@ import SQLite
99

1010
final class Database {
1111
/// Shared instance.
12-
static let shared = Database()
13-
14-
let connection: Connection
15-
16-
private init() {
12+
static let shared: Swift.Result<Database, Error> = {
1713
do {
18-
var url = try FileManager.default.url(
19-
for: .libraryDirectory,
20-
in: .userDomainMask,
21-
appropriateFor: nil, create: true
22-
)
23-
url.appendPathComponent("lcpdatabase.sqlite")
24-
connection = try Connection(url.absoluteString)
14+
return try .success(Database())
2515
} catch {
26-
fatalError("Error initializing db.")
16+
return .failure(error)
2717
}
18+
}()
19+
20+
let connection: Connection
21+
22+
private init() throws {
23+
var url = try FileManager.default.url(
24+
for: .libraryDirectory,
25+
in: .userDomainMask,
26+
appropriateFor: nil, create: true
27+
)
28+
url.appendPathComponent("lcpdatabase.sqlite")
29+
connection = try Connection(url.absoluteString)
2830
}
2931
}
3032

Sources/Adapters/LCPSQLite/SQLiteLCPLicenseRepository.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ public class LCPSQLiteLicenseRepository: LCPLicenseRepository {
1717

1818
private let db: Connection
1919

20-
public init() {
21-
db = Database.shared.connection
20+
public init() throws {
21+
db = try Database.shared.get().connection
2222

23-
_ = try? db.run(licenses.create(temporary: false, ifNotExists: true) { t in
23+
try db.run(licenses.create(temporary: false, ifNotExists: true) { t in
2424
t.column(id, unique: true)
2525
t.column(printsLeft)
2626
t.column(copiesLeft)
2727
})
2828

2929
if db.userVersion == 0 {
30-
_ = try? db.run(licenses.addColumn(registered, defaultValue: false))
30+
try db.run(licenses.addColumn(registered, defaultValue: false))
3131
db.userVersion = 1
3232
}
3333
if db.userVersion == 1 {

Sources/Adapters/LCPSQLite/SQLiteLCPPassphraseRepository.swift

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,15 @@ public class LCPSQLitePassphraseRepository: LCPPassphraseRepository, Loggable {
1818

1919
private let db: Connection
2020

21-
public init() {
22-
db = Database.shared.connection
21+
public init() throws {
22+
db = try Database.shared.get().connection
2323

24-
do {
25-
try db.run(transactions.create(temporary: false, ifNotExists: true) { t in
26-
t.column(licenseId)
27-
t.column(provider)
28-
t.column(userId)
29-
t.column(passphrase)
30-
})
31-
} catch {
32-
log(.error, error)
33-
}
24+
try db.run(transactions.create(temporary: false, ifNotExists: true) { t in
25+
t.column(licenseId)
26+
t.column(provider)
27+
t.column(userId)
28+
t.column(passphrase)
29+
})
3430
}
3531

3632
public func passphrase(for licenseID: LicenseDocument.ID) async throws -> LCPPassphraseHash? {

TestApp/Sources/App/Readium.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ final class Readium {
4545

4646
lazy var lcpService = LCPService(
4747
client: LCPClient(),
48-
licenseRepository: LCPSQLiteLicenseRepository(),
49-
passphraseRepository: LCPSQLitePassphraseRepository(),
48+
licenseRepository: try! LCPSQLiteLicenseRepository(),
49+
passphraseRepository: try! LCPSQLitePassphraseRepository(),
5050
assetRetriever: assetRetriever,
5151
httpClient: httpClient
5252
)

0 commit comments

Comments
 (0)