Improve exception messages 01/26201/3
authorPiotr Bartosiewicz <p.bartosiewi@partner.samsung.com>
Mon, 18 Aug 2014 15:49:46 +0000 (17:49 +0200)
committerJan Olszak <j.olszak@samsung.com>
Tue, 19 Aug 2014 10:46:39 +0000 (03:46 -0700)
[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
src/config/from-json-visitor.hpp
src/config/fs-utils.cpp [new file with mode: 0644]
src/config/fs-utils.hpp [new file with mode: 0644]
src/config/manager.hpp

index 6489c73..5de62a5 100644 (file)
@@ -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
index a3b502d..cc19a8c 100644 (file)
@@ -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 (file)
index 0000000..1b08dce
--- /dev/null
@@ -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 <fstream>
+#include <streambuf>
+
+
+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<size_t>(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<std::streamsize>(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 (file)
index 0000000..924a642
--- /dev/null
@@ -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 <string>
+
+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
index 902633f..a1d7dce 100644 (file)
 #include "config/to-json-visitor.hpp"
 #include "config/from-json-visitor.hpp"
 #include "config/is-visitable.hpp"
-#include "logger/logger.hpp"
-
-#include <fstream>
-#include <streambuf>
+#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<size_t>(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 <class Config>
 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 <class Config>
 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);
     }
 }