Skip to content

Error: Create Error Object with {fmt} #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2023
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
5 changes: 4 additions & 1 deletion error/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@ project(error)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic")
set(CMAKE_CXX_STANDARD 11)

include(cmake/CPM.cmake)
cpmaddpackage("gh:fmtlib/fmt#10.0.0")

add_library(error src/error.cpp)
target_include_directories(error PUBLIC include)
target_link_libraries(error PUBLIC fmt)

if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
include(cmake/CPM.cmake)
cpmaddpackage("gh:TheLartians/[email protected]")

if(BUILD_TESTING)
Expand Down
16 changes: 12 additions & 4 deletions error/include/error/error.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#pragma once

#include <fmt/core.h>

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

namespace error {

Expand All @@ -9,14 +13,18 @@ namespace error {
*/
class Error : public std::exception {
private:
const char* message; /**< The error message. */
std::string message; /**< The error message. */

public:
/**
* @brief Constructs a new error with the given message.
* @param message An error message.
* @brief Constructs a new error 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.
*/
Error(const char* message);
template <typename... T>
Error(fmt::format_string<T...> fmt, T&&... args)
: message(fmt::format(fmt, std::forward<T>(args)...)) {}

/**
* @brief Returns the explanatory string.
Expand Down
4 changes: 1 addition & 3 deletions error/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace error {

Error::Error(const char* message) : message(message) {}

const char* Error::what() const noexcept { return message; }
const char* Error::what() const noexcept { return message.c_str(); }

} // namespace error
11 changes: 9 additions & 2 deletions error/test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,15 @@
#include <string>

TEST_CASE("Error Construction") {
const error::Error err("unknown error");
REQUIRE(std::string("unknown error") == err.what());
SECTION("With one argument") {
const error::Error err("unknown error");
REQUIRE(std::string("unknown error") == err.what());
}

SECTION("With one or more arguments") {
const error::Error err("HTTP error {}", 404);
REQUIRE(std::string("HTTP error 404") == err.what());
}
}

TEST_CASE("Error Throwing and Catching") {
Expand Down