Skip to content
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
14 changes: 8 additions & 6 deletions include/errors/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ class Error {
Error(const std::shared_ptr<const std::string>& message_ptr);

public:

/**
* @brief Constructs an empty error object.
*/
Error();

/**
* @brief Returns the error message.
*
Expand Down Expand Up @@ -54,6 +48,7 @@ class Error {
explicit operator bool() const;

friend Error make(const std::string& msg);
friend const Error& nil();

/**
* @brief Writes the string representation of an error object to the given
Expand Down Expand Up @@ -83,4 +78,11 @@ class Error {
*/
Error make(const std::string& msg);


/**
* @brief Gets a constant reference of an empty error.
* @return A constant reference of an empty error.
*/
const Error& nil();

} // namespace error
7 changes: 5 additions & 2 deletions src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ namespace errors {

Error::Error(const std::shared_ptr<const std::string>& message_ptr) : message_ptr(message_ptr) {}

Error::Error() {}

std::string_view Error::message() const {
if (!message_ptr) return "no error";
return *message_ptr;
Expand All @@ -23,4 +21,9 @@ Error make(const std::string& msg) {
return Error(std::make_shared<const std::string>(msg));
}

const Error& nil() {
static const Error err(nullptr);
return err;
}

} // namespace error
2 changes: 1 addition & 1 deletion test/error_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ TEST_CASE("Error Construction") {
}

TEST_CASE("Empty Error Construction") {
const errors::Error err;
const auto err = errors::nil();
REQUIRE_FALSE(err);
REQUIRE(err.message() == "no error");
}
Expand Down