Better logging for errors
This commit is contained in:
parent
4490b2ca84
commit
7728ff7d35
@ -86,9 +86,14 @@ final class CapServer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private func loadCaps() throws {
|
private func loadCaps() throws {
|
||||||
let data = try Data(contentsOf: dbFile)
|
do {
|
||||||
caps = try JSONDecoder().decode([Cap].self, from: data)
|
let data = try Data(contentsOf: dbFile)
|
||||||
.reduce(into: [:]) { $0[$1.id] = $1 }
|
caps = try JSONDecoder().decode([Cap].self, from: data)
|
||||||
|
.reduce(into: [:]) { $0[$1.id] = $1 }
|
||||||
|
} catch {
|
||||||
|
log("Failed to load caps: \(error)")
|
||||||
|
throw error
|
||||||
|
}
|
||||||
log("\(caps.count) caps loaded")
|
log("\(caps.count) caps loaded")
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -146,10 +151,15 @@ final class CapServer {
|
|||||||
// MARK: Counts
|
// MARK: Counts
|
||||||
|
|
||||||
private func updateCounts() throws {
|
private func updateCounts() throws {
|
||||||
caps = try caps.mapValues {
|
do {
|
||||||
var cap = $0
|
caps = try caps.mapValues {
|
||||||
cap.count = try count(of: $0.id)
|
var cap = $0
|
||||||
return cap
|
cap.count = try count(of: $0.id)
|
||||||
|
return cap
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
log("Failed to update counts: \(error)")
|
||||||
|
throw error
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,13 +13,9 @@ public func configure(_ app: Application) throws {
|
|||||||
.appendingPathComponent("config.json")
|
.appendingPathComponent("config.json")
|
||||||
let config: Config
|
let config: Config
|
||||||
if !FileManager.default.fileExists(atPath: configFile.path) {
|
if !FileManager.default.fileExists(atPath: configFile.path) {
|
||||||
config = .default
|
config = try writeDefaultCofig(to: configFile)
|
||||||
let configData = try JSONEncoder().encode(config)
|
|
||||||
try configData.write(to: configFile)
|
|
||||||
print("Wrote default configuration")
|
|
||||||
} else {
|
} else {
|
||||||
let configData = try Data(contentsOf: configFile)
|
config = try loadConfig(at: configFile)
|
||||||
config = try JSONDecoder().decode(Config.self, from: configData)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try Log.set(logFile: config.logPath)
|
try Log.set(logFile: config.logPath)
|
||||||
@ -36,3 +32,25 @@ public func configure(_ app: Application) throws {
|
|||||||
// Register routes to the router
|
// Register routes to the router
|
||||||
try routes(app)
|
try routes(app)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private func writeDefaultCofig(to path: URL) throws -> Config {
|
||||||
|
do {
|
||||||
|
let configData = try JSONEncoder().encode(Config.default)
|
||||||
|
try configData.write(to: path)
|
||||||
|
print("Wrote default configuration to \(path.path)")
|
||||||
|
return .default
|
||||||
|
} catch {
|
||||||
|
print("Failed to write default config file at \(path.path): \(error)")
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private func loadConfig(at path: URL) throws -> Config {
|
||||||
|
do {
|
||||||
|
let configData = try Data(contentsOf: path)
|
||||||
|
return try JSONDecoder().decode(Config.self, from: configData)
|
||||||
|
} catch {
|
||||||
|
print("Failed to load config file at \(path.path): \(error)")
|
||||||
|
throw error
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user