File tree Expand file tree Collapse file tree 4 files changed +22
-8
lines changed
Expand file tree Collapse file tree 4 files changed +22
-8
lines changed Original file line number Diff line number Diff line change @@ -5,11 +5,14 @@ project(error)
55set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic" )
66set (CMAKE_CXX_STANDARD 11)
77
8+ include (cmake/CPM.cmake)
9+ cpmaddpackage("gh:fmtlib/fmt#10.0.0" )
10+
811add_library (error src/error.cpp)
912target_include_directories (error PUBLIC include )
13+ target_link_libraries (error PUBLIC fmt)
1014
1115if (CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR )
12- include (cmake/CPM.cmake)
1316 cpmaddpackage(
"gh:TheLartians/[email protected] " )
1417
1518 if (BUILD_TESTING)
Original file line number Diff line number Diff line change 11#pragma once
22
3+ #include < fmt/core.h>
4+
35#include < exception>
46#include < string>
7+ #include < utility>
58
69namespace error {
710
@@ -14,10 +17,13 @@ class Error : public std::exception {
1417
1518 public:
1619 /* *
17- * @brief Constructs a new error with the given message.
18- * @param message An error message.
20+ * @brief Constructs a new error with the given format for the message.
21+ * @param fmt A format string for the message.
22+ * @param args Format arguments.
1923 */
20- Error (const char * message);
24+ template <typename ... T>
25+ Error (fmt::format_string<T...> fmt, T&&... args)
26+ : message(fmt::format(fmt, std::forward<T>(args)...)) {}
2127
2228 /* *
2329 * @brief Returns the explanatory string.
Original file line number Diff line number Diff line change 22
33namespace error {
44
5- Error::Error (const char * message) : message(message) {}
6-
75const char * Error::what () const noexcept { return message.c_str (); }
86
97} // namespace error
Original file line number Diff line number Diff line change 33#include < string>
44
55TEST_CASE (" Error Construction" ) {
6- const error::Error err (" unknown error" );
7- REQUIRE (std::string (" unknown error" ) == err.what ());
6+ SECTION (" With one argument" ) {
7+ const error::Error err (" unknown error" );
8+ REQUIRE (std::string (" unknown error" ) == err.what ());
9+ }
10+
11+ SECTION (" With one or more arguments" ) {
12+ const error::Error err (" HTTP error {}" , 404 );
13+ REQUIRE (std::string (" HTTP error 404" ) == err.what ());
14+ }
815}
916
1017TEST_CASE (" Error Throwing and Catching" ) {
You can’t perform that action at this time.
0 commit comments