Skip to content

Commit

Permalink
feat(error): add Error creation support with custom message
Browse files Browse the repository at this point in the history
  • Loading branch information
threeal committed Jun 21, 2023
1 parent 60e3d63 commit ce9b1eb
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
14 changes: 12 additions & 2 deletions error/include/error/error.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,19 @@
namespace error {

/**
* @brief A struct that represents error information.
* @brief A class that represents error information.
*/
struct Error : public std::exception {
class Error : public std::exception {
private:
const char* message; /**< The error message. */

public:
/**
* @brief Constructs a new error with the given message.
* @param message An error message.
*/
Error(const char* message);

/**
* @brief Returns the explanatory string.
* @return Pointer to a null-terminated string with explanatory information.
Expand Down
4 changes: 3 additions & 1 deletion error/src/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace error {

const char* Error::what() const noexcept { return "unknown error"; }
Error::Error(const char* message) : message(message) {}

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

} // namespace error
5 changes: 3 additions & 2 deletions error/test/error_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <catch2/catch_test_macros.hpp>
#include <error/error.hpp>
#include <string>

TEST_CASE("test Error::what") {
const error::Error err;
TEST_CASE("Constructs an error") {
const error::Error err("unknown error");
const std::string expected("unknown error");
REQUIRE(expected.compare(err.what()) == 0);
}

0 comments on commit ce9b1eb

Please # to comment.