Parcourir la source

Add dedicated message constructor to FileOperationException class

Patrick-Christopher Mattulat il y a 2 ans
Parent
commit
b41d11d3f5

+ 6 - 1
include/ls-std/core/exception/FileOperationException.hpp

@@ -3,7 +3,7 @@
  * Company:         Lynar Studios
  * E-Mail:          webmaster@lynarstudios.com
  * Created:         2020-08-15
- * Changed:         2023-02-05
+ * Changed:         2023-02-07
  *
  * */
 
@@ -21,9 +21,14 @@ namespace ls::std::core
     public:
 
       FileOperationException();
+      explicit FileOperationException(::std::string _message);
       ~FileOperationException() override;
 
       [[nodiscard]] const char *what() const noexcept override;
+
+    private:
+
+      ::std::string message{};
   };
 }
 

+ 17 - 2
source/ls-std/core/exception/FileOperationException.cpp

@@ -3,17 +3,32 @@
  * 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/ExceptionMessage.hpp>
 #include <ls-std/core/exception/FileOperationException.hpp>
 
 ls::std::core::FileOperationException::FileOperationException() = default;
 
+ls::std::core::FileOperationException::FileOperationException(::std::string _message) : message(::std::move(_message))
+{}
+
 ls::std::core::FileOperationException::~FileOperationException() = default;
 
 const char *ls::std::core::FileOperationException::what() const noexcept
 {
-  return "FileOperationException thrown - file operation failed!";
+  ::std::string concatenatedMessage = "FileOperationException thrown - ";
+
+  if (this->message.empty())
+  {
+    concatenatedMessage = concatenatedMessage + "file operation failed!";
+  }
+  else
+  {
+    concatenatedMessage = concatenatedMessage + this->message;
+  }
+
+  return ExceptionMessage{concatenatedMessage}.toCharacterPointer();
 }

+ 18 - 0
test/cases/core/exception/FileOperationExceptionTest.cpp

@@ -46,4 +46,22 @@ namespace
         },
         FileOperationException);
   }
+
+  TEST_F(FileOperationExceptionTest, constructor_dedicated_message)
+  {
+    EXPECT_THROW(
+        {
+          try
+          {
+            throw FileOperationException{R"(creating directory "tmp")"};
+          }
+          catch (const FileOperationException &_exception)
+          {
+            ::std::string message = _exception.what();
+            EXPECT_STREQ(R"(FileOperationException thrown - creating directory "tmp")", message.c_str());
+            throw;
+          }
+        },
+        FileOperationException);
+  }
 }