Skip to content

Error: Remove Inheritance of std::exception in Error Struct #33

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 5 commits into from
Jul 8, 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
1 change: 1 addition & 0 deletions error/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ 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)
target_compile_features(error PRIVATE cxx_std_20)

# Check if this project is the main project
if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR)
Expand Down
21 changes: 4 additions & 17 deletions error/include/error/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <fmt/core.h>

#include <exception>
#include <ostream>
#include <string>
#include <utility>
Expand All @@ -12,14 +11,8 @@ namespace error {
/**
* @brief Represents error information.
*/
struct Error : public std::exception {
std::string message; /**< The error message. */

/**
* @brief Constructs a new error object with the given message.
* @param msg An error message.
*/
Error(const std::string& msg);
struct Error {
const std::string message; /**< The error message. */

/**
* @brief Writes the string representation of an error object to the given
Expand All @@ -33,19 +26,13 @@ struct Error : public std::exception {
* stream.
*
* @code{.cpp}
* const error::Error err("unknown error");
* const auto err = error::make("unknown error");
*
* // Print "error: unknown error"
* std::cout << err << std::endl;
* @endcode
*/
friend std::ostream& operator<<(std::ostream& os, const error::Error& err);

/**
* @brief Returns the explanatory string.
* @return Pointer to a null-terminated string with explanatory information.
*/
const char* what() const noexcept override;
};

/**
Expand All @@ -64,7 +51,7 @@ Error make(const std::string& msg);
*/
template <typename... T>
Error format(fmt::format_string<T...> fmt, T&&... args) {
return Error(fmt::format(fmt, std::forward<T>(args)...));
return error::make(fmt::format(fmt, std::forward<T>(args)...));
}

/**
Expand Down
10 changes: 3 additions & 7 deletions error/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@

namespace error {

Error::Error(const std::string& msg) : message(msg) {}

std::ostream& operator<<(std::ostream& os, const error::Error& err) {
return os << "error: " << err.what();
return os << "error: " << err.message;
}

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

Error make(const std::string& msg) { return Error(msg); }
Error make(const std::string& msg) { return Error{.message = msg}; }

bool operator==(const Error& lhs, const Error& rhs) {
return lhs.message == rhs.message;
Expand All @@ -31,7 +27,7 @@ format_parse_context::iterator formatter<error::Error>::parse(

format_context::iterator formatter<error::Error>::format(
const error::Error& err, format_context& ctx) const {
return format_to(ctx.out(), "error: {}", err.what());
return format_to(ctx.out(), "error: {}", err.message);
}

} // namespace fmt
28 changes: 3 additions & 25 deletions error/test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,40 +15,18 @@ TEST_CASE("Error Construction With Formatting") {
REQUIRE(err.message == "HTTP error 404");
}

TEST_CASE("Error Throwing and Catching") {
SECTION("Catch as error::Error") {
try {
throw error::Error("unknown error");
} catch (const error::Error& err) {
REQUIRE(err.message == "unknown error");
} catch (...) {
FAIL("Expected to be caught as error::Error");
}
}

SECTION("Catch as std::exception") {
try {
throw error::Error("unknown error");
} catch (const std::exception& e) {
REQUIRE(std::string("unknown error") == e.what());
} catch (...) {
FAIL("Expected to be caught as std::exception");
}
}
}

TEST_CASE("Error Comparison") {
const auto err = error::Error("unknown error");
const auto err = error::make("unknown error");
const auto err_copy = err;
CHECK(err == err_copy);
CHECK_FALSE(err != err_copy);
const auto other_err = error::Error("other error");
const auto other_err = error::make("other error");
CHECK_FALSE(err == other_err);
CHECK(err != other_err);
}

TEST_CASE("Error Printing") {
const error::Error err("unknown error");
const auto err = error::make("unknown error");

SECTION("Using ostream") {
const auto ss = std::stringstream() << err;
Expand Down