Переглянути джерело

Add dedicated message constructor to FileNotFoundException class

Patrick-Christopher Mattulat 2 роки тому
батько
коміт
957a9f3973

+ 8 - 2
include/ls-std/core/exception/FileNotFoundException.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-17
- * Changed:         2023-02-05
+ * Changed:         2023-02-07
  *
  * */
 
@@ -15,7 +15,6 @@
 #include <ls-std/os/dynamic-goal.hpp>
 #include <string>
 
-//TODO: pass parameters, use class, show class name
 namespace ls::std::core
 {
   class LS_STD_DYNAMIC_GOAL FileNotFoundException : public ::std::exception
@@ -23,9 +22,16 @@ namespace ls::std::core
     public:
 
       FileNotFoundException();
+      explicit FileNotFoundException(::std::string   _message);
       ~FileNotFoundException() override;
 
       [[nodiscard]] const char *what() const noexcept override;
+
+    private:
+
+      ::std::string message{};
+
+      static char* _mapMessageToRawPointer(const ::std::string& _message);
   };
 }
 

+ 26 - 2
source/ls-std/core/exception/FileNotFoundException.cpp

@@ -3,17 +3,41 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2023-02-04
- * Changed:         2023-02-04
+ * Changed:         2023-02-07
  *
  * */
 
 #include <ls-std/core/exception/FileNotFoundException.hpp>
+#include <utility>
 
 ls::std::core::FileNotFoundException::FileNotFoundException() = default;
 
+ls::std::core::FileNotFoundException::FileNotFoundException(::std::string _message) : message(::std::move(_message))
+{}
+
 ls::std::core::FileNotFoundException::~FileNotFoundException() = default;
 
 const char *ls::std::core::FileNotFoundException::what() const noexcept
 {
-  return "FileNotFoundException thrown - file not found!";
+  ::std::string concatenatedMessage = "FileNotFoundException thrown - ";
+
+  if (this->message.empty())
+  {
+    concatenatedMessage = concatenatedMessage + "file not found!";
+  }
+  else
+  {
+    concatenatedMessage = concatenatedMessage + this->message;
+  }
+
+  return ls::std::core::FileNotFoundException::_mapMessageToRawPointer(concatenatedMessage);
+}
+
+char *ls::std::core::FileNotFoundException::_mapMessageToRawPointer(const ::std::string &_message)
+{
+  char *rawPointerMessage = new char[_message.size() + 1];
+  strcpy(rawPointerMessage, _message.c_str());
+  rawPointerMessage[_message.size()] = '\0';
+
+  return rawPointerMessage;
 }

+ 18 - 1
test/cases/core/exception/FileNotFoundExceptionTest.cpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2021-05-01
- * Changed:         2023-02-04
+ * Changed:         2023-02-07
  *
  * */
 
@@ -44,4 +44,21 @@ namespace
         },
         FileNotFoundException);
   }
+
+  TEST_F(FileNotFoundExceptionTest, constructor_dedicated_message)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            throw FileNotFoundException{R"("settings.txt" not found!)"};
+          }
+          catch (const FileNotFoundException &_exception)
+          {
+            EXPECT_STREQ(R"(FileNotFoundException thrown - "settings.txt" not found!)", _exception.what());
+            throw;
+          }
+        },
+        FileNotFoundException);
+  }
 }