File tree Expand file tree Collapse file tree 4 files changed +36
-10
lines changed
Expand file tree Collapse file tree 4 files changed +36
-10
lines changed Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ API Docs
1212
1313.. doxygenfunction :: errors::format
1414
15- .. doxygenstruct :: errors::Error
15+ .. doxygenclass :: errors::Error
1616 :members:
1717
1818License
Original file line number Diff line number Diff line change 22
33#include < fmt/core.h>
44
5+ #include < memory>
56#include < ostream>
67#include < string>
78#include < utility>
@@ -11,8 +12,24 @@ namespace errors {
1112/* *
1213 * @brief Represents error information.
1314 */
14- struct Error {
15- const std::string message; /* *< The error message. */
15+ class Error {
16+ private:
17+ const std::shared_ptr<const std::string> message_ptr;
18+
19+ public:
20+ Error (const std::shared_ptr<const std::string>& message_ptr);
21+
22+ /* *
23+ * @brief Returns the error message.
24+ *
25+ * @code{.cpp}
26+ * const auto err = errors::make("unknown error");
27+ *
28+ * // Print "unknown error"
29+ * std::cout << err << std::endl;
30+ * @endcode
31+ */
32+ std::string message () const ;
1633
1734 /* *
1835 * @brief Writes the string representation of an error object to the given
Original file line number Diff line number Diff line change 22
33namespace errors {
44
5+ Error::Error (const std::shared_ptr<const std::string>& message_ptr) : message_ptr(message_ptr) {}
6+
7+ std::string Error::message () const {
8+ if (!message_ptr) return " no error" ;
9+ return *message_ptr;
10+ }
11+
512std::ostream& operator <<(std::ostream& os, const errors::Error& err) {
6- return os << " error: " << err.message ;
13+ return os << " error: " << err.message () ;
714}
815
916bool operator ==(const Error& lhs, const Error& rhs) {
10- return lhs.message == rhs.message ;
17+ return lhs.message () == rhs.message () ;
1118}
1219
1320bool operator !=(const Error& lhs, const Error& rhs) {
14- return lhs.message != rhs.message ;
21+ return lhs.message () != rhs.message () ;
1522}
1623
17- Error make (const std::string& msg) { return Error{.message = msg}; }
24+ Error make (const std::string& msg) {
25+ return Error (std::make_shared<const std::string>(msg));
26+ }
1827
1928} // namespace error
2029
@@ -27,7 +36,7 @@ format_parse_context::iterator formatter<errors::Error>::parse(
2736
2837format_context::iterator formatter<errors::Error>::format(
2938 const errors::Error& err, format_context& ctx) const {
30- return format_to (ctx.out (), " error: {}" , err.message );
39+ return format_to (ctx.out (), " error: {}" , err.message () );
3140}
3241
3342} // namespace fmt
Original file line number Diff line number Diff line change 77
88TEST_CASE (" Error Construction" ) {
99 const errors::Error err = errors::make (" unknown error" );
10- REQUIRE (err.message == " unknown error" );
10+ REQUIRE (err.message () == " unknown error" );
1111}
1212
1313TEST_CASE (" Error Construction With Formatting" ) {
1414 const errors::Error err = errors::format (" HTTP error {}" , 404 );
15- REQUIRE (err.message == " HTTP error 404" );
15+ REQUIRE (err.message () == " HTTP error 404" );
1616}
1717
1818TEST_CASE (" Error Comparison" ) {
You can’t perform that action at this time.
0 commit comments