From 2b005d8df43ee50889e89ce6797e393ac46c5f87 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 7 Jul 2023 19:39:22 +0700 Subject: [PATCH 1/5] feat(error): remove `Error` struct constructor --- error/include/error/error.hpp | 8 +------- error/src/error.cpp | 8 +++++--- error/test/error_test.cpp | 10 +++++----- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index 5d4dc2b..df4b0cf 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -15,12 +15,6 @@ namespace error { 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); - /** * @brief Writes the string representation of an error object to the given * output stream. @@ -64,7 +58,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..f2e949b 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -2,15 +2,17 @@ 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(); } 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) { + Error err; + err.message = msg; + return err; +} bool operator==(const Error& lhs, const Error& rhs) { return lhs.message == rhs.message; diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 3e9ed3e..32c0506 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -18,7 +18,7 @@ TEST_CASE("Error Construction With Formatting") { TEST_CASE("Error Throwing and Catching") { SECTION("Catch as error::Error") { try { - throw error::Error("unknown error"); + throw error::make("unknown error"); } catch (const error::Error& err) { REQUIRE(err.message == "unknown error"); } catch (...) { @@ -28,7 +28,7 @@ TEST_CASE("Error Throwing and Catching") { SECTION("Catch as std::exception") { try { - throw error::Error("unknown error"); + throw error::make("unknown error"); } catch (const std::exception& e) { REQUIRE(std::string("unknown error") == e.what()); } catch (...) { @@ -38,17 +38,17 @@ TEST_CASE("Error Throwing and Catching") { } 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; From 63e76dba617b3edf5ac37e97fb8464f9cad7fee9 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 7 Jul 2023 19:43:35 +0700 Subject: [PATCH 2/5] docs(error): fix error creation in the ostream operator example --- error/include/error/error.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index df4b0cf..29667bd 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -27,7 +27,7 @@ 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; From b3dd07abe70ec27be5b4745f8b360de1c0c18682 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 7 Jul 2023 22:53:44 +0700 Subject: [PATCH 3/5] feat(error): make the `Error` struct does not inherit `std::exception` --- error/CMakeLists.txt | 1 + error/include/error/error.hpp | 5 ++--- error/src/error.cpp | 6 +----- error/test/error_test.cpp | 22 ---------------------- 4 files changed, 4 insertions(+), 30 deletions(-) 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 29667bd..c5885d4 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -2,7 +2,6 @@ #include -#include #include #include #include @@ -12,7 +11,7 @@ namespace error { /** * @brief Represents error information. */ -struct Error : public std::exception { +struct Error { std::string message; /**< The error message. */ /** @@ -39,7 +38,7 @@ struct Error : public std::exception { * @brief Returns the explanatory string. * @return Pointer to a null-terminated string with explanatory information. */ - const char* what() const noexcept override; + const char* what() const noexcept; }; /** diff --git a/error/src/error.cpp b/error/src/error.cpp index f2e949b..97fdd4b 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -8,11 +8,7 @@ std::ostream& operator<<(std::ostream& os, const error::Error& err) { const char* Error::what() const noexcept { return message.c_str(); } -Error make(const std::string& msg) { - Error err; - err.message = msg; - return err; -} +Error make(const std::string& msg) { return Error{.message = msg}; } bool operator==(const Error& lhs, const Error& rhs) { return lhs.message == rhs.message; diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index 32c0506..a82b05d 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -15,28 +15,6 @@ 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::make("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::make("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::make("unknown error"); const auto err_copy = err; From 2328e8406fea4073c910c9f14332281ae165c632 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 7 Jul 2023 23:04:55 +0700 Subject: [PATCH 4/5] feat(error): remove `Error::what()` method --- error/include/error/error.hpp | 6 ------ error/src/error.cpp | 6 ++---- 2 files changed, 2 insertions(+), 10 deletions(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index c5885d4..9f58c4d 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -33,12 +33,6 @@ struct Error { * @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; }; /** diff --git a/error/src/error.cpp b/error/src/error.cpp index 97fdd4b..04a9c7b 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -3,11 +3,9 @@ namespace error { 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{.message = msg}; } bool operator==(const Error& lhs, const Error& rhs) { @@ -29,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 From ac8f06b68967c66e5d67d4a32f828c0a8bb695e6 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sat, 8 Jul 2023 09:44:07 +0700 Subject: [PATCH 5/5] feat(error): make `Error::message` property to be constant --- error/include/error/error.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index 9f58c4d..f2ef001 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -12,7 +12,7 @@ namespace error { * @brief Represents error information. */ struct Error { - std::string message; /**< The error message. */ + const std::string message; /**< The error message. */ /** * @brief Writes the string representation of an error object to the given