Modify RecordCorruptedException class hierarchy 71/32771/17
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Mon, 22 Dec 2014 15:18:41 +0000 (16:18 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Fri, 27 Feb 2015 14:53:08 +0000 (15:53 +0100)
So far there was only one type of record Cynara could read from its
database. If any corruption was detected, BucketRecordCorruptedException
was thrown.

Now database will contain information about not only buckets and
policies, but also some metadata for them (in this case - checksums).
Need for exception superclass for handling corrupted records emerged.

Patch modifies exception class hierarchy and adds new exception type:
ChecksumRecordCorruptedException.

Change-Id: I3af6bd20b57e17ec31d766f138595920c7a413bd

src/common/exceptions/BucketRecordCorruptedException.h
src/common/exceptions/ChecksumRecordCorruptedException.h [new file with mode: 0644]
src/common/exceptions/RecordCorruptedException.h [new file with mode: 0644]

index a5ffb7d..e7ea604 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-2015 Samsung Electronics Co., Ltd All Rights Reserved
  *
  *    Licensed under the Apache License, Version 2.0 (the "License");
  *    you may not use this file except in compliance with the License.
 /**
  * @file        src/common/exceptions/BucketRecordCorruptedException.h
  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
  * @version     1.0
  * @brief       Implementation of BucketRecordCorruptedException
  */
+
 #ifndef SRC_COMMON_EXCEPTIONS_BUCKETRECORDCORRUPTEDEXCEPTION_H_
 #define SRC_COMMON_EXCEPTIONS_BUCKETRECORDCORRUPTEDEXCEPTION_H_
 
 #include <string>
 
-#include <exceptions/DatabaseException.h>
+#include <exceptions/RecordCorruptedException.h>
 
 namespace Cynara {
 
-class BucketRecordCorruptedException : public DatabaseException {
+class BucketRecordCorruptedException : public RecordCorruptedException {
 public:
-    BucketRecordCorruptedException(void) = delete;
+    BucketRecordCorruptedException() = delete;
     virtual ~BucketRecordCorruptedException() {};
 
-    BucketRecordCorruptedException(const std::string &line)
-        : m_lineNumber(0), m_line(line)  {}
+    BucketRecordCorruptedException(const std::string &line) : RecordCorruptedException(line) {
+        setMessage(subtype());
+    }
 
     BucketRecordCorruptedException withLineNumber(const size_t &lineNumber) const {
         BucketRecordCorruptedException copy(*this);
         copy.m_lineNumber = lineNumber;
-        copy.m_message.clear();
+        copy.setMessage(copy.subtype());
         return copy;
     }
 
     BucketRecordCorruptedException withFilename(const std::string &filename) const {
         BucketRecordCorruptedException copy(*this);
         copy.m_filename = filename;
-        copy.m_message.clear();
+        copy.setMessage(copy.subtype());
         return copy;
     }
 
-    virtual const std::string &message(void) const {
-        if (m_message.empty()) {
-            m_message = "Bucket record corrupted at"
-                + formatedFilename()
-                + formatedLineNumber()
-                + ": <" + slicedLine() + ">";
-        }
-        return m_message;
-    }
-
-    const std::string &filename(void) const {
-        return m_filename;
-    }
-
-    const std::string &line(void) const {
-        return m_line;
-    }
-
-    size_t lineNumber(void) const {
-        return m_lineNumber;
-    }
-
-protected:
-    inline std::string slicedLine(void) const {
-        return m_line.substr(0, 50) + (m_line.size() > 50 ? "..." : "");
-    }
-
-    inline std::string formatedFilename(void) const {
-        return m_filename.empty() ? " line" : " " + m_filename;
-    }
-
-    inline std::string formatedLineNumber(void) const {
-        return m_lineNumber <= 0 ? ""
-                : (m_filename.empty() ? " " : ":")
-                  + std::to_string(static_cast<long int>(m_lineNumber));
-    }
-
 private:
-    size_t m_lineNumber;
-    std::string m_line;
-    std::string m_filename;
-    mutable std::string m_message;
+    const std::string &subtype(void) {
+        static const std::string subtype("Bucket");
+        return subtype;
+    }
 };
 
 } /* namespace Cynara */
diff --git a/src/common/exceptions/ChecksumRecordCorruptedException.h b/src/common/exceptions/ChecksumRecordCorruptedException.h
new file mode 100644 (file)
index 0000000..84b81bc
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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        src/common/exceptions/ChecksumRecordCorruptedException.h
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Implementation of ChecksumRecordCorruptedException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_CHECKSUMRECORDCORRUPTEDEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_CHECKSUMRECORDCORRUPTEDEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/RecordCorruptedException.h>
+
+namespace Cynara {
+
+class ChecksumRecordCorruptedException : public RecordCorruptedException {
+public:
+    ChecksumRecordCorruptedException() = delete;
+    virtual ~ChecksumRecordCorruptedException() {};
+
+    ChecksumRecordCorruptedException(const std::string &line) : RecordCorruptedException(line) {
+        setMessage(subtype());
+    }
+
+    ChecksumRecordCorruptedException withLineNumber(const size_t &lineNumber) const {
+        ChecksumRecordCorruptedException copy(*this);
+        copy.m_lineNumber = lineNumber;
+        copy.setMessage(copy.subtype());
+        return copy;
+    }
+
+private:
+    const std::string &subtype(void) {
+        static const std::string subtype("Checksum");
+        return subtype;
+    }
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_CHECKSUMRECORDCORRUPTEDEXCEPTION_H_ */
diff --git a/src/common/exceptions/RecordCorruptedException.h b/src/common/exceptions/RecordCorruptedException.h
new file mode 100644 (file)
index 0000000..6177aab
--- /dev/null
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    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        src/common/exceptions/RecordCorruptedException.h
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Implementation of RecordCorruptedException
+ */
+
+#ifndef SRC_COMMON_EXCEPTIONS_RECORDCORRUPTEDEXCEPTION_H_
+#define SRC_COMMON_EXCEPTIONS_RECORDCORRUPTEDEXCEPTION_H_
+
+#include <string>
+
+#include <exceptions/DatabaseException.h>
+
+namespace Cynara {
+
+class RecordCorruptedException : public DatabaseException {
+public:
+    RecordCorruptedException(void) = delete;
+    virtual ~RecordCorruptedException() {};
+
+    RecordCorruptedException(const std::string &line)
+        : m_lineNumber(0), m_line(line), m_filename(std::string()) {
+        setMessage(std::string());
+    }
+
+    const std::string &message(void) const {
+        return m_message;
+    }
+
+    const std::string &filename(void) const {
+        return m_filename;
+    }
+
+    const std::string &line(void) const {
+        return m_line;
+    }
+
+    size_t lineNumber(void) const {
+        return m_lineNumber;
+    }
+
+protected:
+    std::string slicedLine(void) const {
+        return m_line.substr(0, 50) + (m_line.size() > 50 ? "..." : "");
+    }
+
+    std::string formattedMetadata(void) const {
+        return (m_filename.empty() ? "line" : m_filename)
+                + (m_lineNumber <= 0 ? "" : formattedLineNumber());
+    }
+
+    std::string formattedLineNumber(void) const {
+        return (m_filename.empty() ? " " : ":")
+                + std::to_string(static_cast<long int>(m_lineNumber));
+    }
+
+    void setMessage(const std::string &subtype) {
+        m_message = (subtype.empty() ? "Record" : subtype + " record") + " corrupted at "
+                    + formattedMetadata() + ": <" + slicedLine() + ">";
+    }
+
+    size_t m_lineNumber;
+    std::string m_line;
+    std::string m_filename;
+    std::string m_message;
+};
+
+} /* namespace Cynara */
+
+#endif /* SRC_COMMON_EXCEPTIONS_RECORDCORRUPTEDEXCEPTION_H_ */