From 38f438de7c1500578f0faf80d1343d7e8cc2cd2f Mon Sep 17 00:00:00 2001 From: Maciek Borzecki Date: Sat, 2 Jul 2016 22:58:49 +0200 Subject: [PATCH] device: loading device from cache may fail Replace named constructor Device.from_cache() with static method Device.new_from_cache() to catch errors when loading from cache and return null in such case. --- src/mconnect/device.vala | 37 +++++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 14 deletions(-) diff --git a/src/mconnect/device.vala b/src/mconnect/device.vala index 0b35016..b3c675a 100644 --- a/src/mconnect/device.vala +++ b/src/mconnect/device.vala @@ -73,23 +73,32 @@ class Device : Object { * @cache: device cache file * @name: device name */ - public Device.from_cache(KeyFile cache, string name) { + public static Device? new_from_cache(KeyFile cache, string name) { debug("device from cache group %s", name); - this.device_id = cache.get_string(name, "deviceId"); - this.device_name = cache.get_string(name, "deviceName"); - this.device_type = cache.get_string(name, "deviceType"); - this.protocol_version = cache.get_integer(name, "protocolVersion"); - this.tcp_port = (uint) cache.get_integer(name, "tcpPort"); - var last_ip_str = cache.get_string(name, "lastIPAddress"); - debug("last known address: %s:%u", last_ip_str, this.tcp_port); - - var host = new InetAddress.from_string(last_ip_str); - if (host == null) { - debug("failed to parse last known IP address (%s) for device %s", - last_ip_str, name); + try { + var dev = new Device(); + dev.device_id = cache.get_string(name, "deviceId"); + dev.device_name = cache.get_string(name, "deviceName"); + dev.device_type = cache.get_string(name, "deviceType"); + dev.protocol_version = cache.get_integer(name, "protocolVersion"); + dev.tcp_port = (uint) cache.get_integer(name, "tcpPort"); + var last_ip_str = cache.get_string(name, "lastIPAddress"); + debug("last known address: %s:%u", last_ip_str, dev.tcp_port); + + var host = new InetAddress.from_string(last_ip_str); + if (host == null) { + debug("failed to parse last known IP address (%s) for device %s", + last_ip_str, name); + return null; + } + dev.host = host; + return dev; + } + catch (KeyFileError e) { + warning("failed to load device data from cache: %s", e.message); + return null; } - this.host = host; } ~Device() {