Skip to content

Commit fd02f4c

Browse files
authored
chore: merge pull request #16 from threeal/simplified-error
Error: Simplify `error::Error`
2 parents 66e8f99 + 8b40d8e commit fd02f4c

File tree

3 files changed

+17
-57
lines changed

3 files changed

+17
-57
lines changed

error/include/error/error.hpp

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,40 @@
33
#include <fmt/core.h>
44

55
#include <exception>
6-
#include <memory>
76
#include <string>
87
#include <utility>
98

109
namespace error {
1110

1211
/**
13-
* @brief A class that represents error information.
12+
* @brief Represents error information.
1413
*/
15-
class Error : public std::exception {
16-
private:
14+
struct Error : public std::exception {
1715
std::string message; /**< The error message. */
1816

19-
public:
2017
/**
21-
* @brief Constructs a new error with the given format for the message.
22-
* @tparam T Variadic template parameter pack for format arguments.
23-
* @param fmt A format string for the message.
24-
* @param args Format arguments.
18+
* @brief Constructs a new error object with the given message.
19+
* @param msg An error message.
2520
*/
26-
template <typename... T>
27-
Error(fmt::format_string<T...> fmt, T&&... args)
28-
: message(fmt::format(fmt, std::forward<T>(args)...)) {}
21+
Error(const std::string& msg);
2922

3023
/**
3124
* @brief Returns the explanatory string.
3225
* @return Pointer to a null-terminated string with explanatory information.
3326
*/
3427
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;
4228
};
4329

4430
/**
45-
* @brief Alias for a shared pointer to the `Error` class.
46-
*/
47-
using ErrorPtr = std::shared_ptr<Error>;
48-
49-
/**
50-
* @brief Creates a new error pointer with the given format for the message.
31+
* @brief Creates a new error object with a formatted message.
5132
* @tparam T Variadic template parameter pack for format arguments.
5233
* @param fmt A format string for the message.
5334
* @param args Format arguments.
54-
* @return Shared pointer to a new error.
35+
* @return A new error object.
5536
*/
5637
template <typename... T>
57-
ErrorPtr make(fmt::format_string<T...> fmt, T&&... args) {
58-
return std::make_shared<Error>(fmt, std::forward<T>(args)...);
38+
Error format(fmt::format_string<T...> fmt, T&&... args) {
39+
return Error(fmt::format(fmt, std::forward<T>(args)...));
5940
}
6041

6142
} // namespace error

error/src/error.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace error {
44

5-
const char* Error::what() const noexcept { return message.c_str(); }
5+
Error::Error(const std::string& msg) : message(msg) {}
66

7-
bool Error::matches(const std::string& str) const noexcept {
8-
return message == str;
9-
}
7+
const char* Error::what() const noexcept { return message.c_str(); }
108

119
} // namespace error

error/test/error_test.cpp

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,21 @@
33
#include <string>
44

55
TEST_CASE("Error Construction") {
6-
SECTION("With one argument") {
7-
const error::Error err("unknown error");
8-
REQUIRE(err.matches("unknown error"));
9-
}
10-
11-
SECTION("With one or more arguments") {
12-
const error::Error err("HTTP error {}", 404);
13-
REQUIRE(err.matches("HTTP error 404"));
14-
}
6+
const error::Error err("unknown error");
7+
REQUIRE(err.message == "unknown error");
158
}
169

17-
TEST_CASE("Error Pointer Construction") {
18-
SECTION("With one argument") {
19-
const error::ErrorPtr err = error::make("unknown error");
20-
REQUIRE(err->matches("unknown error"));
21-
}
22-
23-
SECTION("With one or more arguments") {
24-
const error::ErrorPtr err = error::make("HTTP error {}", 404);
25-
REQUIRE(err->matches("HTTP error 404"));
26-
}
10+
TEST_CASE("Error Construction With Formatting") {
11+
const error::Error err = error::format("HTTP error {}", 404);
12+
REQUIRE(err.message == "HTTP error 404");
2713
}
2814

2915
TEST_CASE("Error Throwing and Catching") {
3016
SECTION("Catch as error::Error") {
3117
try {
3218
throw error::Error("unknown error");
3319
} catch (const error::Error& err) {
34-
REQUIRE(err.matches("unknown error"));
20+
REQUIRE(err.message == "unknown error");
3521
} catch (...) {
3622
FAIL("Expected to be caught as error::Error");
3723
}
@@ -47,8 +33,3 @@ TEST_CASE("Error Throwing and Catching") {
4733
}
4834
}
4935
}
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)