diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index eba1c39..efd0f5b 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -33,4 +34,21 @@ class Error : public std::exception { const char* what() const noexcept override; }; +/** + * @brief Alias for a shared pointer to the `Error` class. + */ +using ErrorPtr = std::shared_ptr; + +/** + * @brief Creates a new error pointer with the given format for the message. + * @tparam T Variadic template parameter pack for format arguments. + * @param fmt A format string for the message. + * @param args Format arguments. + * @return Shared pointer to a new error. + */ +template +ErrorPtr make(fmt::format_string fmt, T&&... args) { + return std::make_shared(fmt, std::forward(args)...); +} + } // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index f1c7755..56943d0 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -14,6 +14,18 @@ TEST_CASE("Error Construction") { } } +TEST_CASE("Error Pointer Construction") { + SECTION("With one argument") { + const error::ErrorPtr err = error::make("unknown error"); + REQUIRE(std::string("unknown error") == err->what()); + } + + SECTION("With one or more arguments") { + const error::ErrorPtr err = error::make("HTTP error {}", 404); + REQUIRE(std::string("HTTP error 404") == err->what()); + } +} + TEST_CASE("Error Throwing and Catching") { SECTION("Catch as error::Error") { try {