diff --git a/error/CMakeLists.txt b/error/CMakeLists.txt index a493130..d9b4656 100644 --- a/error/CMakeLists.txt +++ b/error/CMakeLists.txt @@ -10,6 +10,7 @@ cpmaddpackage("gh:fmtlib/fmt#10.0.0") add_library(error src/error.cpp) target_include_directories(error PUBLIC include) target_link_libraries(error PUBLIC fmt) +target_compile_features(error PRIVATE cxx_std_20) # Check if this project is the main project if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index 5d4dc2b..f2ef001 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -12,14 +11,8 @@ namespace error { /** * @brief Represents error information. */ -struct Error : public std::exception { - std::string message; /**< The error message. */ - - /** - * @brief Constructs a new error object with the given message. - * @param msg An error message. - */ - Error(const std::string& msg); +struct Error { + const std::string message; /**< The error message. */ /** * @brief Writes the string representation of an error object to the given @@ -33,19 +26,13 @@ struct Error : public std::exception { * stream. * * @code{.cpp} - * const error::Error err("unknown error"); + * const auto err = error::make("unknown error"); * * // Print "error: unknown error" * std::cout << err << std::endl; * @endcode */ friend std::ostream& operator<<(std::ostream& os, const error::Error& err); - - /** - * @brief Returns the explanatory string. - * @return Pointer to a null-terminated string with explanatory information. - */ - const char* what() const noexcept override; }; /** @@ -64,7 +51,7 @@ Error make(const std::string& msg); */ template Error format(fmt::format_string fmt, T&&... args) { - return Error(fmt::format(fmt, std::forward(args)...)); + return error::make(fmt::format(fmt, std::forward(args)...)); } /** diff --git a/error/src/error.cpp b/error/src/error.cpp index eb41c2a..04a9c7b 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -2,15 +2,11 @@ namespace error { -Error::Error(const std::string& msg) : message(msg) {} - std::ostream& operator<<(std::ostream& os, const error::Error& err) { - return os << "error: " << err.what(); + return os << "error: " << err.message; } -const char* Error::what() const noexcept { return message.c_str(); } - -Error make(const std::string& msg) { return Error(msg); } +Error make(const std::string& msg) { return Error{.message = msg}; } bool operator==(const Error& lhs, const Error& rhs) { return lhs.message == rhs.message; @@ -31,7 +27,7 @@ format_parse_context::iterator formatter::parse( format_context::iterator formatter::format( const error::Error& err, format_context& ctx) const { - return format_to(ctx.out(), "error: {}", err.what()); + return format_to(ctx.out(), "error: {}", err.message); } } // namespace fmt diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 3e9ed3e..a82b05d 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -15,40 +15,18 @@ TEST_CASE("Error Construction With Formatting") { REQUIRE(err.message == "HTTP error 404"); } -TEST_CASE("Error Throwing and Catching") { - SECTION("Catch as error::Error") { - try { - throw error::Error("unknown error"); - } catch (const error::Error& err) { - REQUIRE(err.message == "unknown error"); - } catch (...) { - FAIL("Expected to be caught as error::Error"); - } - } - - SECTION("Catch as std::exception") { - try { - throw error::Error("unknown error"); - } catch (const std::exception& e) { - REQUIRE(std::string("unknown error") == e.what()); - } catch (...) { - FAIL("Expected to be caught as std::exception"); - } - } -} - TEST_CASE("Error Comparison") { - const auto err = error::Error("unknown error"); + const auto err = error::make("unknown error"); const auto err_copy = err; CHECK(err == err_copy); CHECK_FALSE(err != err_copy); - const auto other_err = error::Error("other error"); + const auto other_err = error::make("other error"); CHECK_FALSE(err == other_err); CHECK(err != other_err); } TEST_CASE("Error Printing") { - const error::Error err("unknown error"); + const auto err = error::make("unknown error"); SECTION("Using ostream") { const auto ss = std::stringstream() << err;