Skip to content

Commit 071bd37

Browse files
authored
chore: merge pull request #33 from threeal/error-no-inherit-std-except
Error: Remove Inheritance of `std::exception` in `Error` Struct
2 parents 8c88ac1 + ac8f06b commit 071bd37

File tree

4 files changed

+11
-49
lines changed

4 files changed

+11
-49
lines changed

error/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ cpmaddpackage("gh:fmtlib/fmt#10.0.0")
1010
add_library(error src/error.cpp)
1111
target_include_directories(error PUBLIC include)
1212
target_link_libraries(error PUBLIC fmt)
13+
target_compile_features(error PRIVATE cxx_std_20)
1314

1415
# Check if this project is the main project
1516
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)

error/include/error/error.hpp

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <fmt/core.h>
44

5-
#include <exception>
65
#include <ostream>
76
#include <string>
87
#include <utility>
@@ -12,14 +11,8 @@ namespace error {
1211
/**
1312
* @brief Represents error information.
1413
*/
15-
struct Error : public std::exception {
16-
std::string message; /**< The error message. */
17-
18-
/**
19-
* @brief Constructs a new error object with the given message.
20-
* @param msg An error message.
21-
*/
22-
Error(const std::string& msg);
14+
struct Error {
15+
const std::string message; /**< The error message. */
2316

2417
/**
2518
* @brief Writes the string representation of an error object to the given
@@ -33,19 +26,13 @@ struct Error : public std::exception {
3326
* stream.
3427
*
3528
* @code{.cpp}
36-
* const error::Error err("unknown error");
29+
* const auto err = error::make("unknown error");
3730
*
3831
* // Print "error: unknown error"
3932
* std::cout << err << std::endl;
4033
* @endcode
4134
*/
4235
friend std::ostream& operator<<(std::ostream& os, const error::Error& err);
43-
44-
/**
45-
* @brief Returns the explanatory string.
46-
* @return Pointer to a null-terminated string with explanatory information.
47-
*/
48-
const char* what() const noexcept override;
4936
};
5037

5138
/**
@@ -64,7 +51,7 @@ Error make(const std::string& msg);
6451
*/
6552
template <typename... T>
6653
Error format(fmt::format_string<T...> fmt, T&&... args) {
67-
return Error(fmt::format(fmt, std::forward<T>(args)...));
54+
return error::make(fmt::format(fmt, std::forward<T>(args)...));
6855
}
6956

7057
/**

error/src/error.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@
22

33
namespace error {
44

5-
Error::Error(const std::string& msg) : message(msg) {}
6-
75
std::ostream& operator<<(std::ostream& os, const error::Error& err) {
8-
return os << "error: " << err.what();
6+
return os << "error: " << err.message;
97
}
108

11-
const char* Error::what() const noexcept { return message.c_str(); }
12-
13-
Error make(const std::string& msg) { return Error(msg); }
9+
Error make(const std::string& msg) { return Error{.message = msg}; }
1410

1511
bool operator==(const Error& lhs, const Error& rhs) {
1612
return lhs.message == rhs.message;
@@ -31,7 +27,7 @@ format_parse_context::iterator formatter<error::Error>::parse(
3127

3228
format_context::iterator formatter<error::Error>::format(
3329
const error::Error& err, format_context& ctx) const {
34-
return format_to(ctx.out(), "error: {}", err.what());
30+
return format_to(ctx.out(), "error: {}", err.message);
3531
}
3632

3733
} // namespace fmt

error/test/error_test.cpp

Lines changed: 3 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,40 +15,18 @@ TEST_CASE("Error Construction With Formatting") {
1515
REQUIRE(err.message == "HTTP error 404");
1616
}
1717

18-
TEST_CASE("Error Throwing and Catching") {
19-
SECTION("Catch as error::Error") {
20-
try {
21-
throw error::Error("unknown error");
22-
} catch (const error::Error& err) {
23-
REQUIRE(err.message == "unknown error");
24-
} catch (...) {
25-
FAIL("Expected to be caught as error::Error");
26-
}
27-
}
28-
29-
SECTION("Catch as std::exception") {
30-
try {
31-
throw error::Error("unknown error");
32-
} catch (const std::exception& e) {
33-
REQUIRE(std::string("unknown error") == e.what());
34-
} catch (...) {
35-
FAIL("Expected to be caught as std::exception");
36-
}
37-
}
38-
}
39-
4018
TEST_CASE("Error Comparison") {
41-
const auto err = error::Error("unknown error");
19+
const auto err = error::make("unknown error");
4220
const auto err_copy = err;
4321
CHECK(err == err_copy);
4422
CHECK_FALSE(err != err_copy);
45-
const auto other_err = error::Error("other error");
23+
const auto other_err = error::make("other error");
4624
CHECK_FALSE(err == other_err);
4725
CHECK(err != other_err);
4826
}
4927

5028
TEST_CASE("Error Printing") {
51-
const error::Error err("unknown error");
29+
const auto err = error::make("unknown error");
5230

5331
SECTION("Using ostream") {
5432
const auto ss = std::stringstream() << err;

0 commit comments

Comments
 (0)