File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 3
3
#include < fmt/core.h>
4
4
5
5
#include < exception>
6
+ #include < memory>
6
7
#include < string>
7
8
#include < utility>
8
9
@@ -33,4 +34,21 @@ class Error : public std::exception {
33
34
const char * what () const noexcept override ;
34
35
};
35
36
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
+
36
54
} // namespace error
Original file line number Diff line number Diff line change @@ -14,6 +14,18 @@ TEST_CASE("Error Construction") {
14
14
}
15
15
}
16
16
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
+
17
29
TEST_CASE (" Error Throwing and Catching" ) {
18
30
SECTION (" Catch as error::Error" ) {
19
31
try {
You can’t perform that action at this time.
0 commit comments