File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff 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+
1729TEST_CASE (" Error Throwing and Catching" ) {
1830 SECTION (" Catch as error::Error" ) {
1931 try {
You can’t perform that action at this time.
0 commit comments