From 73d960b6699a16e28f782e4776427b9501300322 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Dec 2023 01:41:38 +0700 Subject: [PATCH 1/5] feat: modify `Error` from struct to class --- docs/index.rst | 2 +- include/errors/error.hpp | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index 9d40731..a9b7ef0 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -12,7 +12,7 @@ API Docs .. doxygenfunction:: errors::format -.. doxygenstruct:: errors::Error +.. doxygenclass:: errors::Error :members: License diff --git a/include/errors/error.hpp b/include/errors/error.hpp index cddeb39..09e584e 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -11,7 +11,8 @@ namespace errors { /** * @brief Represents error information. */ -struct Error { +class Error { + public: const std::string message; /**< The error message. */ /** From 8c79c9b8dc89f870f997a5d240804fae573d6b66 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Dec 2023 01:50:33 +0700 Subject: [PATCH 2/5] feat: store `message` as a `const std::shared_ptr` --- include/errors/error.hpp | 4 +++- src/error.cpp | 12 +++++++----- test/error_test.cpp | 4 ++-- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index 09e584e..a82876d 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -2,6 +2,7 @@ #include +#include #include #include #include @@ -13,7 +14,8 @@ namespace errors { */ class Error { public: - const std::string message; /**< The error message. */ + /** The shared pointer of the error message. */ + const std::shared_ptr message; /** * @brief Writes the string representation of an error object to the given diff --git a/src/error.cpp b/src/error.cpp index 30c2ef3..d4da101 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -3,18 +3,20 @@ namespace errors { std::ostream& operator<<(std::ostream& os, const errors::Error& err) { - return os << "error: " << err.message; + return os << "error: " << *err.message; } bool operator==(const Error& lhs, const Error& rhs) { - return lhs.message == rhs.message; + return *lhs.message == *rhs.message; } bool operator!=(const Error& lhs, const Error& rhs) { - return lhs.message != rhs.message; + return *lhs.message != *rhs.message; } -Error make(const std::string& msg) { return Error{.message = msg}; } +Error make(const std::string& msg) { + return Error{.message = std::make_shared(msg)}; +} } // namespace error @@ -27,7 +29,7 @@ format_parse_context::iterator formatter::parse( format_context::iterator formatter::format( const errors::Error& err, format_context& ctx) const { - return format_to(ctx.out(), "error: {}", err.message); + return format_to(ctx.out(), "error: {}", *err.message); } } // namespace fmt diff --git a/test/error_test.cpp b/test/error_test.cpp index 28978ba..eb262e3 100644 --- a/test/error_test.cpp +++ b/test/error_test.cpp @@ -7,12 +7,12 @@ TEST_CASE("Error Construction") { const errors::Error err = errors::make("unknown error"); - REQUIRE(err.message == "unknown error"); + REQUIRE(*err.message == "unknown error"); } TEST_CASE("Error Construction With Formatting") { const errors::Error err = errors::format("HTTP error {}", 404); - REQUIRE(err.message == "HTTP error 404"); + REQUIRE(*err.message == "HTTP error 404"); } TEST_CASE("Error Comparison") { From fb7b8608a6c364533cdf51dd5733fba4334bf9b2 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Dec 2023 01:58:24 +0700 Subject: [PATCH 3/5] feat: add `Error::message` function for getting string from the shared pointer --- include/errors/error.hpp | 14 +++++++++++++- src/error.cpp | 15 ++++++++++----- test/error_test.cpp | 4 ++-- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index a82876d..39814a2 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -15,7 +15,19 @@ namespace errors { class Error { public: /** The shared pointer of the error message. */ - const std::shared_ptr message; + const std::shared_ptr message_ptr; + + /** + * @brief Returns the error message. + * + * @code{.cpp} + * const auto err = errors::make("unknown error"); + * + * // Print "unknown error" + * std::cout << err << std::endl; + * @endcode + */ + std::string message() const; /** * @brief Writes the string representation of an error object to the given diff --git a/src/error.cpp b/src/error.cpp index d4da101..c8c4360 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -2,20 +2,25 @@ namespace errors { +std::string Error::message() const { + if (!message_ptr) return "no error"; + return *message_ptr; +} + std::ostream& operator<<(std::ostream& os, const errors::Error& err) { - return os << "error: " << *err.message; + return os << "error: " << err.message(); } bool operator==(const Error& lhs, const Error& rhs) { - return *lhs.message == *rhs.message; + return lhs.message() == rhs.message(); } bool operator!=(const Error& lhs, const Error& rhs) { - return *lhs.message != *rhs.message; + return lhs.message() != rhs.message(); } Error make(const std::string& msg) { - return Error{.message = std::make_shared(msg)}; + return Error{.message_ptr = std::make_shared(msg)}; } } // namespace error @@ -29,7 +34,7 @@ format_parse_context::iterator formatter::parse( format_context::iterator formatter::format( const errors::Error& err, format_context& ctx) const { - return format_to(ctx.out(), "error: {}", *err.message); + return format_to(ctx.out(), "error: {}", err.message()); } } // namespace fmt diff --git a/test/error_test.cpp b/test/error_test.cpp index eb262e3..bc24dbd 100644 --- a/test/error_test.cpp +++ b/test/error_test.cpp @@ -7,12 +7,12 @@ TEST_CASE("Error Construction") { const errors::Error err = errors::make("unknown error"); - REQUIRE(*err.message == "unknown error"); + REQUIRE(err.message() == "unknown error"); } TEST_CASE("Error Construction With Formatting") { const errors::Error err = errors::format("HTTP error {}", 404); - REQUIRE(*err.message == "HTTP error 404"); + REQUIRE(err.message() == "HTTP error 404"); } TEST_CASE("Error Comparison") { From ece9fb8e7cf7b057bbdd6c7086ee830ba251126d Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Dec 2023 02:05:56 +0700 Subject: [PATCH 4/5] feat: add `Error::Error(const std::shared_ptr&)` constructor --- include/errors/error.hpp | 2 ++ src/error.cpp | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index 39814a2..a7337d7 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -17,6 +17,8 @@ class Error { /** The shared pointer of the error message. */ const std::shared_ptr message_ptr; + Error(const std::shared_ptr& message_ptr); + /** * @brief Returns the error message. * diff --git a/src/error.cpp b/src/error.cpp index c8c4360..0e111fd 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -2,6 +2,8 @@ namespace errors { +Error::Error(const std::shared_ptr& message_ptr) : message_ptr(message_ptr) {} + std::string Error::message() const { if (!message_ptr) return "no error"; return *message_ptr; @@ -20,7 +22,7 @@ bool operator!=(const Error& lhs, const Error& rhs) { } Error make(const std::string& msg) { - return Error{.message_ptr = std::make_shared(msg)}; + return Error(std::make_shared(msg)); } } // namespace error From ae0d2fcec48561f5b838f918680235b7599df3ad Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Sun, 24 Dec 2023 02:07:43 +0700 Subject: [PATCH 5/5] feat: modify `Error::message_ptr` to be private --- include/errors/error.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/errors/error.hpp b/include/errors/error.hpp index a7337d7..26628b3 100644 --- a/include/errors/error.hpp +++ b/include/errors/error.hpp @@ -13,10 +13,10 @@ namespace errors { * @brief Represents error information. */ class Error { - public: - /** The shared pointer of the error message. */ + private: const std::shared_ptr message_ptr; + public: Error(const std::shared_ptr& message_ptr); /**