From ae6bc9063abe8ac7c30d5c7fcab1f6746c241da9 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Fri, 7 Jul 2023 19:19:50 +0700 Subject: [PATCH] feat(error): add a `make` function to create a new error --- error/include/error/error.hpp | 7 +++++++ error/src/error.cpp | 2 ++ error/test/error_test.cpp | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index 2be232b..5d4dc2b 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -48,6 +48,13 @@ struct Error : public std::exception { const char* what() const noexcept override; }; +/** + * @brief Creates a new error object with the given message. + * @param msg The error message. + * @return A new error object. + */ +Error make(const std::string& msg); + /** * @brief Creates a new error object with a formatted message. * @tparam T Variadic template parameter pack for format arguments. diff --git a/error/src/error.cpp b/error/src/error.cpp index c3b119b..eb41c2a 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -10,6 +10,8 @@ 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) { return Error(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 c4fc7a0..3e9ed3e 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -6,7 +6,7 @@ #include TEST_CASE("Error Construction") { - const error::Error err("unknown error"); + const error::Error err = error::make("unknown error"); REQUIRE(err.message == "unknown error"); }