diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index c5b97b0..e83a317 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -3,59 +3,40 @@ #include #include -#include #include #include namespace error { /** - * @brief A class that represents error information. + * @brief Represents error information. */ -class Error : public std::exception { - private: +struct Error : public std::exception { std::string message; /**< The error message. */ - public: /** - * @brief Constructs a new error with the given format for the message. - * @tparam T Variadic template parameter pack for format arguments. - * @param fmt A format string for the message. - * @param args Format arguments. + * @brief Constructs a new error object with the given message. + * @param msg An error message. */ - template - Error(fmt::format_string fmt, T&&... args) - : message(fmt::format(fmt, std::forward(args)...)) {} + Error(const std::string& msg); /** * @brief Returns the explanatory string. * @return Pointer to a null-terminated string with explanatory information. */ const char* what() const noexcept override; - - /** - * @brief Checks if the error message matches the given string. - * @param str A string to be matched. - * @return True if it matches, false otherwise. - */ - bool matches(const std::string& str) const noexcept; }; /** - * @brief Alias for a shared pointer to the `Error` class. - */ -using ErrorPtr = std::shared_ptr; - -/** - * @brief Creates a new error pointer with the given format for the message. + * @brief Creates a new error object with a formatted message. * @tparam T Variadic template parameter pack for format arguments. * @param fmt A format string for the message. * @param args Format arguments. - * @return Shared pointer to a new error. + * @return A new error object. */ template -ErrorPtr make(fmt::format_string fmt, T&&... args) { - return std::make_shared(fmt, std::forward(args)...); +Error format(fmt::format_string fmt, T&&... args) { + return Error(fmt::format(fmt, std::forward(args)...)); } } // namespace error diff --git a/error/src/error.cpp b/error/src/error.cpp index 310a91a..4e3d0b0 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -2,10 +2,8 @@ namespace error { -const char* Error::what() const noexcept { return message.c_str(); } +Error::Error(const std::string& msg) : message(msg) {} -bool Error::matches(const std::string& str) const noexcept { - return message == str; -} +const char* Error::what() const noexcept { return message.c_str(); } } // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index e0067df..2504d47 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -3,27 +3,13 @@ #include TEST_CASE("Error Construction") { - SECTION("With one argument") { - const error::Error err("unknown error"); - REQUIRE(err.matches("unknown error")); - } - - SECTION("With one or more arguments") { - const error::Error err("HTTP error {}", 404); - REQUIRE(err.matches("HTTP error 404")); - } + const error::Error err("unknown error"); + REQUIRE(err.message == "unknown error"); } -TEST_CASE("Error Pointer Construction") { - SECTION("With one argument") { - const error::ErrorPtr err = error::make("unknown error"); - REQUIRE(err->matches("unknown error")); - } - - SECTION("With one or more arguments") { - const error::ErrorPtr err = error::make("HTTP error {}", 404); - REQUIRE(err->matches("HTTP error 404")); - } +TEST_CASE("Error Construction With Formatting") { + const error::Error err = error::format("HTTP error {}", 404); + REQUIRE(err.message == "HTTP error 404"); } TEST_CASE("Error Throwing and Catching") { @@ -31,7 +17,7 @@ TEST_CASE("Error Throwing and Catching") { try { throw error::Error("unknown error"); } catch (const error::Error& err) { - REQUIRE(err.matches("unknown error")); + REQUIRE(err.message == "unknown error"); } catch (...) { FAIL("Expected to be caught as error::Error"); } @@ -47,8 +33,3 @@ TEST_CASE("Error Throwing and Catching") { } } } - -TEST_CASE("Error Message Matching") { - const error::Error err("unknown error"); - REQUIRE(err.matches("unknown error")); -}