Skip to content

Commit 3b10c64

Browse files
authored
chore: merge pull request #11 from threeal/error-shared-ptr
Error: Add Alias for Shared Pointer of `Error`.
2 parents 7af81f1 + 1501ae6 commit 3b10c64

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

error/include/error/error.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <fmt/core.h>
44

55
#include <exception>
6+
#include <memory>
67
#include <string>
78
#include <utility>
89

@@ -33,4 +34,21 @@ class Error : public std::exception {
3334
const char* what() const noexcept override;
3435
};
3536

37+
/**
38+
* @brief Alias for a shared pointer to the `Error` class.
39+
*/
40+
using ErrorPtr = std::shared_ptr<Error>;
41+
42+
/**
43+
* @brief Creates a new error pointer with the given format for the message.
44+
* @tparam T Variadic template parameter pack for format arguments.
45+
* @param fmt A format string for the message.
46+
* @param args Format arguments.
47+
* @return Shared pointer to a new error.
48+
*/
49+
template <typename... T>
50+
ErrorPtr make(fmt::format_string<T...> fmt, T&&... args) {
51+
return std::make_shared<Error>(fmt, std::forward<T>(args)...);
52+
}
53+
3654
} // namespace error

error/test/error_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ TEST_CASE("Error Construction") {
1414
}
1515
}
1616

17+
TEST_CASE("Error Pointer Construction") {
18+
SECTION("With one argument") {
19+
const error::ErrorPtr err = error::make("unknown error");
20+
REQUIRE(std::string("unknown error") == err->what());
21+
}
22+
23+
SECTION("With one or more arguments") {
24+
const error::ErrorPtr err = error::make("HTTP error {}", 404);
25+
REQUIRE(std::string("HTTP error 404") == err->what());
26+
}
27+
}
28+
1729
TEST_CASE("Error Throwing and Catching") {
1830
SECTION("Catch as error::Error") {
1931
try {

0 commit comments

Comments
 (0)