diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index efd0f5b..c5b97b0 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -32,6 +32,13 @@ class Error : public std::exception { * @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; }; /** diff --git a/error/src/error.cpp b/error/src/error.cpp index 6d6310e..310a91a 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -4,4 +4,8 @@ namespace error { const char* Error::what() const noexcept { return message.c_str(); } +bool Error::matches(const std::string& str) const noexcept { + return message == str; +} + } // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 56943d0..e0067df 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -5,24 +5,24 @@ TEST_CASE("Error Construction") { SECTION("With one argument") { const error::Error err("unknown error"); - REQUIRE(std::string("unknown error") == err.what()); + REQUIRE(err.matches("unknown error")); } SECTION("With one or more arguments") { const error::Error err("HTTP error {}", 404); - REQUIRE(std::string("HTTP error 404") == err.what()); + REQUIRE(err.matches("HTTP error 404")); } } TEST_CASE("Error Pointer Construction") { SECTION("With one argument") { const error::ErrorPtr err = error::make("unknown error"); - REQUIRE(std::string("unknown error") == err->what()); + REQUIRE(err->matches("unknown error")); } SECTION("With one or more arguments") { const error::ErrorPtr err = error::make("HTTP error {}", 404); - REQUIRE(std::string("HTTP error 404") == err->what()); + REQUIRE(err->matches("HTTP error 404")); } } @@ -31,7 +31,7 @@ TEST_CASE("Error Throwing and Catching") { try { throw error::Error("unknown error"); } catch (const error::Error& err) { - REQUIRE(std::string("unknown error") == err.what()); + REQUIRE(err.matches("unknown error")); } catch (...) { FAIL("Expected to be caught as error::Error"); } @@ -47,3 +47,8 @@ TEST_CASE("Error Throwing and Catching") { } } } + +TEST_CASE("Error Message Matching") { + const error::Error err("unknown error"); + REQUIRE(err.matches("unknown error")); +}