Skip to content

Commit 66e8f99

Browse files
authored
chore: merge pull request #15 from threeal/add-error-match
Error: Add `Error::matches` Method
2 parents 3b10c64 + 2712675 commit 66e8f99

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

error/include/error/error.hpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class Error : public std::exception {
3232
* @return Pointer to a null-terminated string with explanatory information.
3333
*/
3434
const char* what() const noexcept override;
35+
36+
/**
37+
* @brief Checks if the error message matches the given string.
38+
* @param str A string to be matched.
39+
* @return True if it matches, false otherwise.
40+
*/
41+
bool matches(const std::string& str) const noexcept;
3542
};
3643

3744
/**

error/src/error.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,8 @@ namespace error {
44

55
const char* Error::what() const noexcept { return message.c_str(); }
66

7+
bool Error::matches(const std::string& str) const noexcept {
8+
return message == str;
9+
}
10+
711
} // namespace error

error/test/error_test.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,24 @@
55
TEST_CASE("Error Construction") {
66
SECTION("With one argument") {
77
const error::Error err("unknown error");
8-
REQUIRE(std::string("unknown error") == err.what());
8+
REQUIRE(err.matches("unknown error"));
99
}
1010

1111
SECTION("With one or more arguments") {
1212
const error::Error err("HTTP error {}", 404);
13-
REQUIRE(std::string("HTTP error 404") == err.what());
13+
REQUIRE(err.matches("HTTP error 404"));
1414
}
1515
}
1616

1717
TEST_CASE("Error Pointer Construction") {
1818
SECTION("With one argument") {
1919
const error::ErrorPtr err = error::make("unknown error");
20-
REQUIRE(std::string("unknown error") == err->what());
20+
REQUIRE(err->matches("unknown error"));
2121
}
2222

2323
SECTION("With one or more arguments") {
2424
const error::ErrorPtr err = error::make("HTTP error {}", 404);
25-
REQUIRE(std::string("HTTP error 404") == err->what());
25+
REQUIRE(err->matches("HTTP error 404"));
2626
}
2727
}
2828

@@ -31,7 +31,7 @@ TEST_CASE("Error Throwing and Catching") {
3131
try {
3232
throw error::Error("unknown error");
3333
} catch (const error::Error& err) {
34-
REQUIRE(std::string("unknown error") == err.what());
34+
REQUIRE(err.matches("unknown error"));
3535
} catch (...) {
3636
FAIL("Expected to be caught as error::Error");
3737
}
@@ -47,3 +47,8 @@ TEST_CASE("Error Throwing and Catching") {
4747
}
4848
}
4949
}
50+
51+
TEST_CASE("Error Message Matching") {
52+
const error::Error err("unknown error");
53+
REQUIRE(err.matches("unknown error"));
54+
}

0 commit comments

Comments
 (0)