Fix: crash when parse config request fail

This commit is contained in:
yicheng 2019-01-12 20:13:32 +08:00
parent f161f3d4dd
commit bdcbb93122
2 changed files with 7 additions and 4 deletions

View File

@ -52,8 +52,11 @@ class ApiRequest{
req("/configs").responseData{
res in
guard let data = res.result.value else {return}
let config = ClashConfig.fromData(data)
completeHandler(config)
if let config = ClashConfig.fromData(data) {
completeHandler(config)
} else {
NSUserNotificationCenter.default.post(title: "Error", info: "Get clash config failed")
}
}
}

View File

@ -32,10 +32,10 @@ class ClashConfig:Codable {
case port, socketPort = "socks-port", allowLan = "allow-lan", mode, logLevel = "log-level"
}
static func fromData(_ data:Data)->ClashConfig{
static func fromData(_ data:Data)->ClashConfig?{
let decoder = JSONDecoder()
let model = try? decoder.decode(ClashConfig.self, from: data)
return model!
return model
}
func copy() -> ClashConfig? {