]> git.ipfire.org Git - thirdparty/snapper.git/commitdiff
Improve error handling for CmdMetaParse
authorCheng-Ling Lai <jameslai.tech@gmail.com>
Tue, 4 Aug 2026 02:12:36 +0000 (10:12 +0800)
committerCheng-Ling Lai <jameslai.tech@gmail.com>
Tue, 4 Aug 2026 03:07:10 +0000 (11:07 +0800)
client/snbk/CmdMetaParse.cc
client/snbk/CmdMetaParse.h

index a5451af09e197fd4f0617752e1ac54c577d27bc1..d1b8dabae5680b5c4e887d3e0aa615015e39cb77 100644 (file)
@@ -53,6 +53,8 @@ namespace snapper
                y2err(tmp);
            for (const string& tmp : cmd.get_stderr())
                y2err(tmp);
+
+           SN_THROW(Exception(_("Failed to load info.xml.")));
        }
 
        content = boost::join(cmd.get_stdout(), "");
@@ -64,7 +66,10 @@ namespace snapper
     string CmdMetaParse::get_checksum() const
     {
        if (!content.length())
-           return "";
+       {
+           y2err(sformat("The content of %s is empty.", path.c_str()));
+           SN_THROW(Exception(_("The content of info.xml is empty.")));
+       }
 
        unsigned char hash[SHA256_DIGEST_LENGTH];
        SHA256(reinterpret_cast<const unsigned char*>(content.c_str()), content.length(),
@@ -83,37 +88,38 @@ namespace snapper
 
     SnapshotMeta CmdMetaParse::get_meta() const
     {
-       SnapshotMeta meta;
-
        if (!content.length())
        {
-           // The `info.xml` file might be missing.
-           // Using fallback values for snapshot metadata.
-           return meta;
+           y2err(sformat("The content of %s is empty.", path.c_str()));
+           SN_THROW(Exception(_("The content of info.xml is empty.")));
        }
 
+       SnapshotMeta meta;
+
        XmlFile file(XmlFile::FromString, content);
        const xmlNode* node = file.getRootElement();
        string tmp;
 
        if (!getChildValue(node, "type", tmp) || !toValue(tmp, meta.type, true))
        {
-           // Log error for the missing attribute and retain the default value.
            y2err("The type attribute is missing from " << path);
+           SN_THROW(Exception(_("The type attribute is missing from `info.xml`.")));
        }
 
        getChildValue(node, "pre_num", meta.pre_num);
        getChildValue(node, "cleanup", meta.cleanup);
 
-       for (const xmlNode* tmp : getChildNodes(node, "userdata"))
+       for (const xmlNode* tmp_node : getChildNodes(node, "userdata"))
        {
            string key, value;
-           getChildValue(tmp, "key", key);
-           getChildValue(tmp, "value", value);
+           getChildValue(tmp_node, "key", key);
+           getChildValue(tmp_node, "value", value);
            if (!key.empty())
                meta.userdata[key] = value;
        }
 
+       meta.state = SnapshotMeta::State::VALID;
+
        return meta;
     }
 
index 790cd3c0b4bb65d6e0229c23710539c684ce327c..5341895ad99a86aa9807e6fbb5ca6ca5749de15f 100644 (file)
@@ -34,8 +34,27 @@ namespace snapper
     using std::string;
 
 
-    struct SnapshotMeta
+    class SnapshotMeta
     {
+    public:
+
+       enum class State
+       {
+           /**
+            * Errors occurred while processing the content of `info.xml`. Either the file
+            * does not exist or some attributes are missing. The state of `SnapshotMeta`
+            * is invalid.
+            */
+           INVALID,
+
+           /**
+            * The content of `info.xml` has been successfully processed. The state of
+            * `SnapshotMeta` is valid.
+            */
+           VALID
+       };
+
+       State state = State::INVALID;
        SnapshotType type = SnapshotType::SINGLE;
        unsigned int pre_num = 0;
        string cleanup;