From d16fd8cfae6d8254a60f0e172269e92201a40e89 Mon Sep 17 00:00:00 2001 From: John Knox Date: Thu, 6 Sep 2018 09:46:22 -0700 Subject: [PATCH] Handle json parse failures Summary: Loading the deviceId is not critical, so if it ever fails, we can live with that by swallowing the exception and using the default (possibly 'unknown' deviceId). Reviewed By: danielbuechele Differential Revision: D9682886 fbshipit-source-id: e5d60dd262fce683dd444167edd1475e0c029759 --- xplat/Sonar/ConnectionContextStore.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/xplat/Sonar/ConnectionContextStore.cpp b/xplat/Sonar/ConnectionContextStore.cpp index 8dd9a2634..6c89cdb8e 100644 --- a/xplat/Sonar/ConnectionContextStore.cpp +++ b/xplat/Sonar/ConnectionContextStore.cpp @@ -64,9 +64,13 @@ std::string ConnectionContextStore::getDeviceId() { desktop app. For backwards compatibility, when this isn't present, fall back to the unreliable source. */ - std::string config = loadStringFromFile(absoluteFilePath(CONNECTION_CONFIG_FILE)); - auto maybeDeviceId = folly::parseJson(config)["deviceId"]; - return maybeDeviceId.isString() ? maybeDeviceId.getString() : deviceData_.deviceId; + try { + std::string config = loadStringFromFile(absoluteFilePath(CONNECTION_CONFIG_FILE)); + auto maybeDeviceId = folly::parseJson(config)["deviceId"]; + return maybeDeviceId.isString() ? maybeDeviceId.getString() : deviceData_.deviceId; + } catch (std::exception& e) { + return deviceData_.deviceId; + } } void ConnectionContextStore::storeConnectionConfig(folly::dynamic& config) {