From f89381d62981d6f3fbb33910f5a374f8724d78cd Mon Sep 17 00:00:00 2001 From: Piotr Bartosiewicz Date: Mon, 18 Aug 2014 17:49:46 +0200 Subject: [PATCH] Improve exception messages [Bug/Feature] Poor exception messages while loading configuration from file. Also too much implementation was placed inside header file. [Cause] N/A [Solution] N/A [Verification] Build, install, run tests Change-Id: Ia187e04788048a1e1c4b7a1d147c5c8b1865da90 --- src/config/exception.hpp | 2 +- src/config/from-json-visitor.hpp | 2 +- src/config/fs-utils.cpp | 73 ++++++++++++++++++++++++++++++++++++++++ src/config/fs-utils.hpp | 39 +++++++++++++++++++++ src/config/manager.hpp | 63 +++++----------------------------- 5 files changed, 122 insertions(+), 57 deletions(-) create mode 100644 src/config/fs-utils.cpp create mode 100644 src/config/fs-utils.hpp diff --git a/src/config/exception.hpp b/src/config/exception.hpp index 6489c73..5de62a5 100644 --- a/src/config/exception.hpp +++ b/src/config/exception.hpp @@ -36,7 +36,7 @@ namespace config { */ struct ConfigException: public std::runtime_error { - ConfigException(const std::string& error = "") : std::runtime_error(error) {} + ConfigException(const std::string& error) : std::runtime_error(error) {} }; } // namespace config diff --git a/src/config/from-json-visitor.hpp b/src/config/from-json-visitor.hpp index a3b502d..cc19a8c 100644 --- a/src/config/from-json-visitor.hpp +++ b/src/config/from-json-visitor.hpp @@ -60,7 +60,7 @@ public: { json_object* object = nullptr; if (!json_object_object_get_ex(mObject, name.c_str(), &object)) { - throw ConfigException("Missing field '" + name + "' in json"); + throw ConfigException("Missing field '" + name + "'"); } fromJsonObject(object, value); } diff --git a/src/config/fs-utils.cpp b/src/config/fs-utils.cpp new file mode 100644 index 0000000..1b08dce --- /dev/null +++ b/src/config/fs-utils.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file + * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) + * @brief Filesystem helper functions + */ + +#include "config/fs-utils.hpp" + +#include +#include + + +namespace config { +namespace fsutils { + +bool readFileContent(const std::string& path, std::string& result) +{ + std::ifstream file(path); + + if (!file) { + return false; + } + + file.seekg(0, std::ios::end); + std::streampos length = file.tellg(); + if (length < 0) { + return false; + } + result.resize(static_cast(length)); + file.seekg(0, std::ios::beg); + + file.read(&result[0], length); + if (!file) { + result.clear(); + return false; + } + + return true; +} + +bool saveFileContent(const std::string& path, const std::string& content) +{ + std::ofstream file(path); + if (!file) { + return false; + } + file.write(content.data(), static_cast(content.size())); + if (!file) { + return false; + } + return true; +} + +} // namespace fsutils +} // namespace config diff --git a/src/config/fs-utils.hpp b/src/config/fs-utils.hpp new file mode 100644 index 0000000..924a642 --- /dev/null +++ b/src/config/fs-utils.hpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved + * + * Contact: Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License + */ + +/** + * @file + * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com) + * @brief src/config/manager.hpp + */ + +#ifndef CONFIG_FS_UTILS_HPP +#define CONFIG_FS_UTILS_HPP + +#include + +namespace config { +namespace fsutils { + +bool readFileContent(const std::string& path, std::string& result); +bool saveFileContent(const std::string& path, const std::string& content); + +} // namespace fsutils +} // namespace config + +#endif // CONFIG_FS_UTILS_HPP diff --git a/src/config/manager.hpp b/src/config/manager.hpp index 902633f..a1d7dce 100644 --- a/src/config/manager.hpp +++ b/src/config/manager.hpp @@ -28,10 +28,7 @@ #include "config/to-json-visitor.hpp" #include "config/from-json-visitor.hpp" #include "config/is-visitable.hpp" -#include "logger/logger.hpp" - -#include -#include +#include "config/fs-utils.hpp" namespace config { @@ -55,61 +52,17 @@ std::string saveToString(const Config& config) return visitor.toString(); } -static bool readFileContent(const std::string& path, std::string& result) -{ - std::ifstream file(path); - - if (!file) { - LOGD(path << ": could not open for reading"); - return false; - } - - file.seekg(0, std::ios::end); - std::streampos length = file.tellg(); - if (length < 0) { - LOGD(path << ": tellg failed"); - return false; - } - result.resize(static_cast(length)); - file.seekg(0, std::ios::beg); - - file.read(&result[0], length); - if (!file) { - LOGD(path << ": read error"); - result.clear(); - return false; - } - - return true; -} - -static bool saveFileContent(const std::string& path, const std::string& content) -{ - std::ofstream file(path); - if (!file) { - LOGD(path << ": could not open for writing"); - return false; - } - file.write(content.data(), content.size()); - if (!file) { - LOGD(path << ": could not write to"); - return false; - } - return true; -} - template void loadFromFile(const std::string& filename, Config& config) { + std::string content; + if (!fsutils::readFileContent(filename, content)) { + throw ConfigException("Could not load " + filename); + } try { - std::string content; - if (!readFileContent(filename, content)) { - throw ConfigException("Could not load " + filename); - } loadFromString(content, config); - } - catch(...) { - throw ConfigException("Could not load configuration."); + } catch (ConfigException& e) { + throw ConfigException("Error in " + filename + ": " + e.what()); } } @@ -117,7 +70,7 @@ template void saveToFile(const std::string& filename, const Config& config) { const std::string content = saveToString(config); - if (!saveFileContent(filename, content)) { + if (!fsutils::saveFileContent(filename, content)) { throw ConfigException("Could not save " + filename); } } -- 2.7.4