Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions error/include/error/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <fmt/core.h>

#include <exception>
#include <memory>
#include <string>
#include <utility>

Expand Down Expand Up @@ -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<Error>;

/**
* @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 <typename... T>
ErrorPtr make(fmt::format_string<T...> fmt, T&&... args) {
return std::make_shared<Error>(fmt, std::forward<T>(args)...);
}

} // namespace error
12 changes: 12 additions & 0 deletions error/test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down